1 //===-- SBTypeSynthetic.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/SBTypeSynthetic.h"
10 #include "SBReproducerPrivate.h"
11
12 #include "lldb/API/SBStream.h"
13
14 #include "lldb/DataFormatters/DataVisualization.h"
15
16 using namespace lldb;
17 using namespace lldb_private;
18
SBTypeSynthetic()19 SBTypeSynthetic::SBTypeSynthetic() : m_opaque_sp() {
20 LLDB_RECORD_CONSTRUCTOR_NO_ARGS(SBTypeSynthetic);
21 }
22
CreateWithClassName(const char * data,uint32_t options)23 SBTypeSynthetic SBTypeSynthetic::CreateWithClassName(const char *data,
24 uint32_t options) {
25 LLDB_RECORD_STATIC_METHOD(lldb::SBTypeSynthetic, SBTypeSynthetic,
26 CreateWithClassName, (const char *, uint32_t), data,
27 options);
28
29 if (!data || data[0] == 0)
30 return LLDB_RECORD_RESULT(SBTypeSynthetic());
31 return LLDB_RECORD_RESULT(SBTypeSynthetic(ScriptedSyntheticChildrenSP(
32 new ScriptedSyntheticChildren(options, data, ""))));
33 }
34
CreateWithScriptCode(const char * data,uint32_t options)35 SBTypeSynthetic SBTypeSynthetic::CreateWithScriptCode(const char *data,
36 uint32_t options) {
37 LLDB_RECORD_STATIC_METHOD(lldb::SBTypeSynthetic, SBTypeSynthetic,
38 CreateWithScriptCode, (const char *, uint32_t),
39 data, options);
40
41 if (!data || data[0] == 0)
42 return LLDB_RECORD_RESULT(SBTypeSynthetic());
43 return LLDB_RECORD_RESULT(SBTypeSynthetic(ScriptedSyntheticChildrenSP(
44 new ScriptedSyntheticChildren(options, "", data))));
45 }
46
SBTypeSynthetic(const lldb::SBTypeSynthetic & rhs)47 SBTypeSynthetic::SBTypeSynthetic(const lldb::SBTypeSynthetic &rhs)
48 : m_opaque_sp(rhs.m_opaque_sp) {
49 LLDB_RECORD_CONSTRUCTOR(SBTypeSynthetic, (const lldb::SBTypeSynthetic &),
50 rhs);
51 }
52
53 SBTypeSynthetic::~SBTypeSynthetic() = default;
54
IsValid() const55 bool SBTypeSynthetic::IsValid() const {
56 LLDB_RECORD_METHOD_CONST_NO_ARGS(bool, SBTypeSynthetic, IsValid);
57 return this->operator bool();
58 }
operator bool() const59 SBTypeSynthetic::operator bool() const {
60 LLDB_RECORD_METHOD_CONST_NO_ARGS(bool, SBTypeSynthetic, operator bool);
61
62 return m_opaque_sp.get() != nullptr;
63 }
64
IsClassCode()65 bool SBTypeSynthetic::IsClassCode() {
66 LLDB_RECORD_METHOD_NO_ARGS(bool, SBTypeSynthetic, IsClassCode);
67
68 if (!IsValid())
69 return false;
70 const char *code = m_opaque_sp->GetPythonCode();
71 return (code && *code);
72 }
73
IsClassName()74 bool SBTypeSynthetic::IsClassName() {
75 LLDB_RECORD_METHOD_NO_ARGS(bool, SBTypeSynthetic, IsClassName);
76
77 if (!IsValid())
78 return false;
79 return !IsClassCode();
80 }
81
GetData()82 const char *SBTypeSynthetic::GetData() {
83 LLDB_RECORD_METHOD_NO_ARGS(const char *, SBTypeSynthetic, GetData);
84
85 if (!IsValid())
86 return nullptr;
87 if (IsClassCode())
88 return m_opaque_sp->GetPythonCode();
89 else
90 return m_opaque_sp->GetPythonClassName();
91 }
92
SetClassName(const char * data)93 void SBTypeSynthetic::SetClassName(const char *data) {
94 LLDB_RECORD_METHOD(void, SBTypeSynthetic, SetClassName, (const char *), data);
95
96 if (IsValid() && data && *data)
97 m_opaque_sp->SetPythonClassName(data);
98 }
99
SetClassCode(const char * data)100 void SBTypeSynthetic::SetClassCode(const char *data) {
101 LLDB_RECORD_METHOD(void, SBTypeSynthetic, SetClassCode, (const char *), data);
102
103 if (IsValid() && data && *data)
104 m_opaque_sp->SetPythonCode(data);
105 }
106
GetOptions()107 uint32_t SBTypeSynthetic::GetOptions() {
108 LLDB_RECORD_METHOD_NO_ARGS(uint32_t, SBTypeSynthetic, GetOptions);
109
110 if (!IsValid())
111 return lldb::eTypeOptionNone;
112 return m_opaque_sp->GetOptions();
113 }
114
SetOptions(uint32_t value)115 void SBTypeSynthetic::SetOptions(uint32_t value) {
116 LLDB_RECORD_METHOD(void, SBTypeSynthetic, SetOptions, (uint32_t), value);
117
118 if (!CopyOnWrite_Impl())
119 return;
120 m_opaque_sp->SetOptions(value);
121 }
122
GetDescription(lldb::SBStream & description,lldb::DescriptionLevel description_level)123 bool SBTypeSynthetic::GetDescription(lldb::SBStream &description,
124 lldb::DescriptionLevel description_level) {
125 LLDB_RECORD_METHOD(bool, SBTypeSynthetic, GetDescription,
126 (lldb::SBStream &, lldb::DescriptionLevel), description,
127 description_level);
128
129 if (m_opaque_sp) {
130 description.Printf("%s\n", m_opaque_sp->GetDescription().c_str());
131 return true;
132 }
133 return false;
134 }
135
136 lldb::SBTypeSynthetic &SBTypeSynthetic::
operator =(const lldb::SBTypeSynthetic & rhs)137 operator=(const lldb::SBTypeSynthetic &rhs) {
138 LLDB_RECORD_METHOD(lldb::SBTypeSynthetic &,
139 SBTypeSynthetic, operator=,(const lldb::SBTypeSynthetic &),
140 rhs);
141
142 if (this != &rhs) {
143 m_opaque_sp = rhs.m_opaque_sp;
144 }
145 return LLDB_RECORD_RESULT(*this);
146 }
147
operator ==(lldb::SBTypeSynthetic & rhs)148 bool SBTypeSynthetic::operator==(lldb::SBTypeSynthetic &rhs) {
149 LLDB_RECORD_METHOD(
150 bool, SBTypeSynthetic, operator==,(lldb::SBTypeSynthetic &), rhs);
151
152 if (!IsValid())
153 return !rhs.IsValid();
154 return m_opaque_sp == rhs.m_opaque_sp;
155 }
156
IsEqualTo(lldb::SBTypeSynthetic & rhs)157 bool SBTypeSynthetic::IsEqualTo(lldb::SBTypeSynthetic &rhs) {
158 LLDB_RECORD_METHOD(bool, SBTypeSynthetic, IsEqualTo,
159 (lldb::SBTypeSynthetic &), rhs);
160
161 if (!IsValid())
162 return !rhs.IsValid();
163
164 if (m_opaque_sp->IsScripted() != rhs.m_opaque_sp->IsScripted())
165 return false;
166
167 if (IsClassCode() != rhs.IsClassCode())
168 return false;
169
170 if (strcmp(GetData(), rhs.GetData()))
171 return false;
172
173 return GetOptions() == rhs.GetOptions();
174 }
175
operator !=(lldb::SBTypeSynthetic & rhs)176 bool SBTypeSynthetic::operator!=(lldb::SBTypeSynthetic &rhs) {
177 LLDB_RECORD_METHOD(
178 bool, SBTypeSynthetic, operator!=,(lldb::SBTypeSynthetic &), rhs);
179
180 if (!IsValid())
181 return !rhs.IsValid();
182 return m_opaque_sp != rhs.m_opaque_sp;
183 }
184
GetSP()185 lldb::ScriptedSyntheticChildrenSP SBTypeSynthetic::GetSP() {
186 return m_opaque_sp;
187 }
188
SetSP(const lldb::ScriptedSyntheticChildrenSP & TypeSynthetic_impl_sp)189 void SBTypeSynthetic::SetSP(
190 const lldb::ScriptedSyntheticChildrenSP &TypeSynthetic_impl_sp) {
191 m_opaque_sp = TypeSynthetic_impl_sp;
192 }
193
SBTypeSynthetic(const lldb::ScriptedSyntheticChildrenSP & TypeSynthetic_impl_sp)194 SBTypeSynthetic::SBTypeSynthetic(
195 const lldb::ScriptedSyntheticChildrenSP &TypeSynthetic_impl_sp)
196 : m_opaque_sp(TypeSynthetic_impl_sp) {}
197
CopyOnWrite_Impl()198 bool SBTypeSynthetic::CopyOnWrite_Impl() {
199 if (!IsValid())
200 return false;
201 if (m_opaque_sp.unique())
202 return true;
203
204 ScriptedSyntheticChildrenSP new_sp(new ScriptedSyntheticChildren(
205 m_opaque_sp->GetOptions(), m_opaque_sp->GetPythonClassName(),
206 m_opaque_sp->GetPythonCode()));
207
208 SetSP(new_sp);
209
210 return true;
211 }
212
213 namespace lldb_private {
214 namespace repro {
215
216 template <>
RegisterMethods(Registry & R)217 void RegisterMethods<SBTypeSynthetic>(Registry &R) {
218 LLDB_REGISTER_CONSTRUCTOR(SBTypeSynthetic, ());
219 LLDB_REGISTER_STATIC_METHOD(lldb::SBTypeSynthetic, SBTypeSynthetic,
220 CreateWithClassName, (const char *, uint32_t));
221 LLDB_REGISTER_STATIC_METHOD(lldb::SBTypeSynthetic, SBTypeSynthetic,
222 CreateWithScriptCode, (const char *, uint32_t));
223 LLDB_REGISTER_CONSTRUCTOR(SBTypeSynthetic, (const lldb::SBTypeSynthetic &));
224 LLDB_REGISTER_METHOD_CONST(bool, SBTypeSynthetic, IsValid, ());
225 LLDB_REGISTER_METHOD_CONST(bool, SBTypeSynthetic, operator bool, ());
226 LLDB_REGISTER_METHOD(bool, SBTypeSynthetic, IsClassCode, ());
227 LLDB_REGISTER_METHOD(bool, SBTypeSynthetic, IsClassName, ());
228 LLDB_REGISTER_METHOD(const char *, SBTypeSynthetic, GetData, ());
229 LLDB_REGISTER_METHOD(void, SBTypeSynthetic, SetClassName, (const char *));
230 LLDB_REGISTER_METHOD(void, SBTypeSynthetic, SetClassCode, (const char *));
231 LLDB_REGISTER_METHOD(uint32_t, SBTypeSynthetic, GetOptions, ());
232 LLDB_REGISTER_METHOD(void, SBTypeSynthetic, SetOptions, (uint32_t));
233 LLDB_REGISTER_METHOD(bool, SBTypeSynthetic, GetDescription,
234 (lldb::SBStream &, lldb::DescriptionLevel));
235 LLDB_REGISTER_METHOD(
236 lldb::SBTypeSynthetic &,
237 SBTypeSynthetic, operator=,(const lldb::SBTypeSynthetic &));
238 LLDB_REGISTER_METHOD(bool,
239 SBTypeSynthetic, operator==,(lldb::SBTypeSynthetic &));
240 LLDB_REGISTER_METHOD(bool, SBTypeSynthetic, IsEqualTo,
241 (lldb::SBTypeSynthetic &));
242 LLDB_REGISTER_METHOD(bool,
243 SBTypeSynthetic, operator!=,(lldb::SBTypeSynthetic &));
244 }
245
246 }
247 }
248