[PowerPC] Collect some CallLowering arguments into a struct. [NFC]
[llvm-project.git] / lldb / source / API / SBStructuredData.cpp
blob6b973e82c858b787b21672e28a2db6661c2bbb62
1 //===-- SBStructuredData.cpp ------------------------------------*- C++ -*-===//
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 //===----------------------------------------------------------------------===//
9 #include "lldb/API/SBStructuredData.h"
10 #include "SBReproducerPrivate.h"
12 #include "lldb/API/SBStream.h"
13 #include "lldb/API/SBStringList.h"
14 #include "lldb/Core/StructuredDataImpl.h"
15 #include "lldb/Target/StructuredDataPlugin.h"
16 #include "lldb/Utility/Event.h"
17 #include "lldb/Utility/Status.h"
18 #include "lldb/Utility/Stream.h"
19 #include "lldb/Utility/StructuredData.h"
21 using namespace lldb;
22 using namespace lldb_private;
24 #pragma mark--
25 #pragma mark SBStructuredData
27 SBStructuredData::SBStructuredData() : m_impl_up(new StructuredDataImpl()) {
28 LLDB_RECORD_CONSTRUCTOR_NO_ARGS(SBStructuredData);
31 SBStructuredData::SBStructuredData(const lldb::SBStructuredData &rhs)
32 : m_impl_up(new StructuredDataImpl(*rhs.m_impl_up.get())) {
33 LLDB_RECORD_CONSTRUCTOR(SBStructuredData, (const lldb::SBStructuredData &),
34 rhs);
37 SBStructuredData::SBStructuredData(const lldb::EventSP &event_sp)
38 : m_impl_up(new StructuredDataImpl(event_sp)) {
39 LLDB_RECORD_CONSTRUCTOR(SBStructuredData, (const lldb::EventSP &), event_sp);
42 SBStructuredData::SBStructuredData(lldb_private::StructuredDataImpl *impl)
43 : m_impl_up(impl) {
44 LLDB_RECORD_CONSTRUCTOR(SBStructuredData,
45 (lldb_private::StructuredDataImpl *), impl);
48 SBStructuredData::~SBStructuredData() {}
50 SBStructuredData &SBStructuredData::
51 operator=(const lldb::SBStructuredData &rhs) {
52 LLDB_RECORD_METHOD(
53 lldb::SBStructuredData &,
54 SBStructuredData, operator=,(const lldb::SBStructuredData &), rhs);
56 *m_impl_up = *rhs.m_impl_up;
57 return LLDB_RECORD_RESULT(*this);
60 lldb::SBError SBStructuredData::SetFromJSON(lldb::SBStream &stream) {
61 LLDB_RECORD_METHOD(lldb::SBError, SBStructuredData, SetFromJSON,
62 (lldb::SBStream &), stream);
64 lldb::SBError error;
65 std::string json_str(stream.GetData());
67 StructuredData::ObjectSP json_obj = StructuredData::ParseJSON(json_str);
68 m_impl_up->SetObjectSP(json_obj);
70 if (!json_obj || json_obj->GetType() != eStructuredDataTypeDictionary)
71 error.SetErrorString("Invalid Syntax");
72 return LLDB_RECORD_RESULT(error);
75 bool SBStructuredData::IsValid() const {
76 LLDB_RECORD_METHOD_CONST_NO_ARGS(bool, SBStructuredData, IsValid);
77 return this->operator bool();
79 SBStructuredData::operator bool() const {
80 LLDB_RECORD_METHOD_CONST_NO_ARGS(bool, SBStructuredData, operator bool);
82 return m_impl_up->IsValid();
85 void SBStructuredData::Clear() {
86 LLDB_RECORD_METHOD_NO_ARGS(void, SBStructuredData, Clear);
88 m_impl_up->Clear();
91 SBError SBStructuredData::GetAsJSON(lldb::SBStream &stream) const {
92 LLDB_RECORD_METHOD_CONST(lldb::SBError, SBStructuredData, GetAsJSON,
93 (lldb::SBStream &), stream);
95 SBError error;
96 error.SetError(m_impl_up->GetAsJSON(stream.ref()));
97 return LLDB_RECORD_RESULT(error);
100 lldb::SBError SBStructuredData::GetDescription(lldb::SBStream &stream) const {
101 LLDB_RECORD_METHOD_CONST(lldb::SBError, SBStructuredData, GetDescription,
102 (lldb::SBStream &), stream);
104 Status error = m_impl_up->GetDescription(stream.ref());
105 SBError sb_error;
106 sb_error.SetError(error);
107 return LLDB_RECORD_RESULT(sb_error);
110 StructuredDataType SBStructuredData::GetType() const {
111 LLDB_RECORD_METHOD_CONST_NO_ARGS(lldb::StructuredDataType, SBStructuredData,
112 GetType);
114 return (m_impl_up ? m_impl_up->GetType() : eStructuredDataTypeInvalid);
117 size_t SBStructuredData::GetSize() const {
118 LLDB_RECORD_METHOD_CONST_NO_ARGS(size_t, SBStructuredData, GetSize);
120 return (m_impl_up ? m_impl_up->GetSize() : 0);
123 bool SBStructuredData::GetKeys(lldb::SBStringList &keys) const {
124 LLDB_RECORD_METHOD_CONST(bool, SBStructuredData, GetKeys,
125 (lldb::SBStringList &), keys);
127 if (!m_impl_up)
128 return false;
130 if (GetType() != eStructuredDataTypeDictionary)
131 return false;
133 StructuredData::ObjectSP obj_sp = m_impl_up->GetObjectSP();
134 if (!obj_sp)
135 return false;
137 StructuredData::Dictionary *dict = obj_sp->GetAsDictionary();
138 // We claimed we were a dictionary, so this can't be null.
139 assert(dict);
140 // The return kind of GetKeys is an Array:
141 StructuredData::ObjectSP array_sp = dict->GetKeys();
142 StructuredData::Array *key_arr = array_sp->GetAsArray();
143 assert(key_arr);
145 key_arr->ForEach([&keys] (StructuredData::Object *object) -> bool {
146 llvm::StringRef key = object->GetStringValue("");
147 keys.AppendString(key.str().c_str());
148 return true;
150 return true;
153 lldb::SBStructuredData SBStructuredData::GetValueForKey(const char *key) const {
154 LLDB_RECORD_METHOD_CONST(lldb::SBStructuredData, SBStructuredData,
155 GetValueForKey, (const char *), key);
157 if (!m_impl_up)
158 return LLDB_RECORD_RESULT(SBStructuredData());
160 SBStructuredData result;
161 result.m_impl_up->SetObjectSP(m_impl_up->GetValueForKey(key));
162 return LLDB_RECORD_RESULT(result);
165 lldb::SBStructuredData SBStructuredData::GetItemAtIndex(size_t idx) const {
166 LLDB_RECORD_METHOD_CONST(lldb::SBStructuredData, SBStructuredData,
167 GetItemAtIndex, (size_t), idx);
169 if (!m_impl_up)
170 return LLDB_RECORD_RESULT(SBStructuredData());
172 SBStructuredData result;
173 result.m_impl_up->SetObjectSP(m_impl_up->GetItemAtIndex(idx));
174 return LLDB_RECORD_RESULT(result);
177 uint64_t SBStructuredData::GetIntegerValue(uint64_t fail_value) const {
178 LLDB_RECORD_METHOD_CONST(uint64_t, SBStructuredData, GetIntegerValue,
179 (uint64_t), fail_value);
181 return (m_impl_up ? m_impl_up->GetIntegerValue(fail_value) : fail_value);
184 double SBStructuredData::GetFloatValue(double fail_value) const {
185 LLDB_RECORD_METHOD_CONST(double, SBStructuredData, GetFloatValue, (double),
186 fail_value);
188 return (m_impl_up ? m_impl_up->GetFloatValue(fail_value) : fail_value);
191 bool SBStructuredData::GetBooleanValue(bool fail_value) const {
192 LLDB_RECORD_METHOD_CONST(bool, SBStructuredData, GetBooleanValue, (bool),
193 fail_value);
195 return (m_impl_up ? m_impl_up->GetBooleanValue(fail_value) : fail_value);
198 size_t SBStructuredData::GetStringValue(char *dst, size_t dst_len) const {
199 LLDB_RECORD_METHOD_CONST(size_t, SBStructuredData, GetStringValue,
200 (char *, size_t), dst, dst_len);
202 return (m_impl_up ? m_impl_up->GetStringValue(dst, dst_len) : 0);
205 namespace lldb_private {
206 namespace repro {
208 template <>
209 void RegisterMethods<SBStructuredData>(Registry &R) {
210 LLDB_REGISTER_CONSTRUCTOR(SBStructuredData, ());
211 LLDB_REGISTER_CONSTRUCTOR(SBStructuredData,
212 (const lldb::SBStructuredData &));
213 LLDB_REGISTER_CONSTRUCTOR(SBStructuredData, (const lldb::EventSP &));
214 LLDB_REGISTER_CONSTRUCTOR(SBStructuredData,
215 (lldb_private::StructuredDataImpl *));
216 LLDB_REGISTER_METHOD(
217 lldb::SBStructuredData &,
218 SBStructuredData, operator=,(const lldb::SBStructuredData &));
219 LLDB_REGISTER_METHOD(lldb::SBError, SBStructuredData, SetFromJSON,
220 (lldb::SBStream &));
221 LLDB_REGISTER_METHOD_CONST(bool, SBStructuredData, IsValid, ());
222 LLDB_REGISTER_METHOD_CONST(bool, SBStructuredData, operator bool, ());
223 LLDB_REGISTER_METHOD(void, SBStructuredData, Clear, ());
224 LLDB_REGISTER_METHOD_CONST(lldb::SBError, SBStructuredData, GetAsJSON,
225 (lldb::SBStream &));
226 LLDB_REGISTER_METHOD_CONST(lldb::SBError, SBStructuredData, GetDescription,
227 (lldb::SBStream &));
228 LLDB_REGISTER_METHOD_CONST(lldb::StructuredDataType, SBStructuredData,
229 GetType, ());
230 LLDB_REGISTER_METHOD_CONST(size_t, SBStructuredData, GetSize, ());
231 LLDB_REGISTER_METHOD_CONST(bool, SBStructuredData, GetKeys,
232 (lldb::SBStringList &));
233 LLDB_REGISTER_METHOD_CONST(lldb::SBStructuredData, SBStructuredData,
234 GetValueForKey, (const char *));
235 LLDB_REGISTER_METHOD_CONST(lldb::SBStructuredData, SBStructuredData,
236 GetItemAtIndex, (size_t));
237 LLDB_REGISTER_METHOD_CONST(uint64_t, SBStructuredData, GetIntegerValue,
238 (uint64_t));
239 LLDB_REGISTER_METHOD_CONST(double, SBStructuredData, GetFloatValue,
240 (double));
241 LLDB_REGISTER_METHOD_CONST(bool, SBStructuredData, GetBooleanValue, (bool));
242 LLDB_REGISTER_METHOD_CONST(size_t, SBStructuredData, GetStringValue,
243 (char *, size_t));