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 
19 SBTypeSynthetic::SBTypeSynthetic() : m_opaque_sp() {
20   LLDB_RECORD_CONSTRUCTOR_NO_ARGS(SBTypeSynthetic);
SBTypeSummaryOptions()21 }
22 
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 
35 SBTypeSynthetic SBTypeSynthetic::CreateWithScriptCode(const char *data,
36                                                       uint32_t options) {
IsValid()37   LLDB_RECORD_STATIC_METHOD(lldb::SBTypeSynthetic, SBTypeSynthetic,
38                             CreateWithScriptCode, (const char *, uint32_t),
39                             data, options);
40 
operator bool() const41   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 
GetLanguage()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 
55 bool SBTypeSynthetic::IsValid() const {
GetCapping()56   LLDB_RECORD_METHOD_CONST_NO_ARGS(bool, SBTypeSynthetic, IsValid);
57   return this->operator bool();
58 }
59 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 
SetLanguage(lldb::LanguageType l)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 }
SetCapping(lldb::TypeSummaryCapping c)73 
74 bool SBTypeSynthetic::IsClassName() {
75   LLDB_RECORD_METHOD_NO_ARGS(bool, SBTypeSynthetic, IsClassName);
76 
77   if (!IsValid())
78     return false;
79   return !IsClassCode();
80 }
operator ->()81 
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 
93 void SBTypeSynthetic::SetClassName(const char *data) {
ref()94   LLDB_RECORD_METHOD(void, SBTypeSynthetic, SetClassName, (const char *), data);
95 
96   if (IsValid() && data && *data)
97     m_opaque_sp->SetPythonClassName(data);
ref() const98 }
99 
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 
107 uint32_t SBTypeSynthetic::GetOptions() {
108   LLDB_RECORD_METHOD_NO_ARGS(uint32_t, SBTypeSynthetic, GetOptions);
109 
110   if (!IsValid())
SetOptions(const lldb_private::TypeSummaryOptions * lldb_object_ptr)111     return lldb::eTypeOptionNone;
112   return m_opaque_sp->GetOptions();
113 }
114 
115 void SBTypeSynthetic::SetOptions(uint32_t value) {
116   LLDB_RECORD_METHOD(void, SBTypeSynthetic, SetOptions, (uint32_t), value);
117 
118   if (!CopyOnWrite_Impl())
SBTypeSummary()119     return;
120   m_opaque_sp->SetOptions(value);
121 }
122 
CreateWithSummaryString(const char * data,uint32_t options)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 
CreateWithFunctionName(const char * data,uint32_t options)136 lldb::SBTypeSynthetic &SBTypeSynthetic::
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 
148 bool SBTypeSynthetic::operator==(lldb::SBTypeSynthetic &rhs) {
CreateWithScriptCode(const char * data,uint32_t options)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 
157 bool SBTypeSynthetic::IsEqualTo(lldb::SBTypeSynthetic &rhs) {
158   LLDB_RECORD_METHOD(bool, SBTypeSynthetic, IsEqualTo,
159                      (lldb::SBTypeSynthetic &), rhs);
160 
161   if (!IsValid())
CreateWithCallback(FormatCallback cb,uint32_t options,const char * description)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 }
__anonf431bb210102(ValueObject &valobj, Stream &stm, const TypeSummaryOptions &opt) 175 
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 
185 lldb::ScriptedSyntheticChildrenSP SBTypeSynthetic::GetSP() {
186   return m_opaque_sp;
187 }
188 
189 void SBTypeSynthetic::SetSP(
SBTypeSummary(const lldb::SBTypeSummary & rhs)190     const lldb::ScriptedSyntheticChildrenSP &TypeSynthetic_impl_sp) {
191   m_opaque_sp = TypeSynthetic_impl_sp;
192 }
193 
194 SBTypeSynthetic::SBTypeSynthetic(
195     const lldb::ScriptedSyntheticChildrenSP &TypeSynthetic_impl_sp)
196     : m_opaque_sp(TypeSynthetic_impl_sp) {}
IsValid() const197 
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 <>
217 void RegisterMethods<SBTypeSynthetic>(Registry &R) {
218   LLDB_REGISTER_CONSTRUCTOR(SBTypeSynthetic, ());
219   LLDB_REGISTER_STATIC_METHOD(lldb::SBTypeSynthetic, SBTypeSynthetic,
IsFunctionName()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));
IsSummaryString()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 &));
GetData()242   LLDB_REGISTER_METHOD(bool,
243                        SBTypeSynthetic, operator!=,(lldb::SBTypeSynthetic &));
244 }
245 
246 }
247 }
248