1 //===-- SBTypeSynthetic.cpp -----------------------------------------*- C++
2 //-*-===//
3 //
4 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
5 // See https://llvm.org/LICENSE.txt for license information.
6 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //
8 //===----------------------------------------------------------------------===//
9 
10 #include "lldb/API/SBTypeSynthetic.h"
11 #include "SBReproducerPrivate.h"
12 
13 #include "lldb/API/SBStream.h"
14 
15 #include "lldb/DataFormatters/DataVisualization.h"
16 
17 using namespace lldb;
18 using namespace lldb_private;
19 
20 #ifndef LLDB_DISABLE_PYTHON
21 
22 SBTypeSynthetic::SBTypeSynthetic() : m_opaque_sp() {
23   LLDB_RECORD_CONSTRUCTOR_NO_ARGS(SBTypeSynthetic);
24 }
25 
26 SBTypeSynthetic SBTypeSynthetic::CreateWithClassName(const char *data,
27                                                      uint32_t options) {
28   LLDB_RECORD_STATIC_METHOD(lldb::SBTypeSynthetic, SBTypeSynthetic,
29                             CreateWithClassName, (const char *, uint32_t), data,
30                             options);
31 
32   if (!data || data[0] == 0)
33     return LLDB_RECORD_RESULT(SBTypeSynthetic());
34   return LLDB_RECORD_RESULT(SBTypeSynthetic(ScriptedSyntheticChildrenSP(
35       new ScriptedSyntheticChildren(options, data, ""))));
36 }
37 
38 SBTypeSynthetic SBTypeSynthetic::CreateWithScriptCode(const char *data,
39                                                       uint32_t options) {
40   LLDB_RECORD_STATIC_METHOD(lldb::SBTypeSynthetic, SBTypeSynthetic,
41                             CreateWithScriptCode, (const char *, uint32_t),
42                             data, options);
43 
44   if (!data || data[0] == 0)
45     return LLDB_RECORD_RESULT(SBTypeSynthetic());
46   return LLDB_RECORD_RESULT(SBTypeSynthetic(ScriptedSyntheticChildrenSP(
47       new ScriptedSyntheticChildren(options, "", data))));
48 }
49 
50 SBTypeSynthetic::SBTypeSynthetic(const lldb::SBTypeSynthetic &rhs)
51     : m_opaque_sp(rhs.m_opaque_sp) {
52   LLDB_RECORD_CONSTRUCTOR(SBTypeSynthetic, (const lldb::SBTypeSynthetic &),
53                           rhs);
54 }
55 
56 SBTypeSynthetic::~SBTypeSynthetic() {}
57 
58 bool SBTypeSynthetic::IsValid() const {
59   LLDB_RECORD_METHOD_CONST_NO_ARGS(bool, SBTypeSynthetic, IsValid);
60   return this->operator bool();
61 }
62 SBTypeSynthetic::operator bool() const {
63   LLDB_RECORD_METHOD_CONST_NO_ARGS(bool, SBTypeSynthetic, operator bool);
64 
65   return m_opaque_sp.get() != NULL;
66 }
67 
68 bool SBTypeSynthetic::IsClassCode() {
69   LLDB_RECORD_METHOD_NO_ARGS(bool, SBTypeSynthetic, IsClassCode);
70 
71   if (!IsValid())
72     return false;
73   const char *code = m_opaque_sp->GetPythonCode();
74   return (code && *code);
75 }
76 
77 bool SBTypeSynthetic::IsClassName() {
78   LLDB_RECORD_METHOD_NO_ARGS(bool, SBTypeSynthetic, IsClassName);
79 
80   if (!IsValid())
81     return false;
82   return !IsClassCode();
83 }
84 
85 const char *SBTypeSynthetic::GetData() {
86   LLDB_RECORD_METHOD_NO_ARGS(const char *, SBTypeSynthetic, GetData);
87 
88   if (!IsValid())
89     return NULL;
90   if (IsClassCode())
91     return m_opaque_sp->GetPythonCode();
92   else
93     return m_opaque_sp->GetPythonClassName();
94 }
95 
96 void SBTypeSynthetic::SetClassName(const char *data) {
97   LLDB_RECORD_METHOD(void, SBTypeSynthetic, SetClassName, (const char *), data);
98 
99   if (IsValid() && data && *data)
100     m_opaque_sp->SetPythonClassName(data);
101 }
102 
103 void SBTypeSynthetic::SetClassCode(const char *data) {
104   LLDB_RECORD_METHOD(void, SBTypeSynthetic, SetClassCode, (const char *), data);
105 
106   if (IsValid() && data && *data)
107     m_opaque_sp->SetPythonCode(data);
108 }
109 
110 uint32_t SBTypeSynthetic::GetOptions() {
111   LLDB_RECORD_METHOD_NO_ARGS(uint32_t, SBTypeSynthetic, GetOptions);
112 
113   if (!IsValid())
114     return lldb::eTypeOptionNone;
115   return m_opaque_sp->GetOptions();
116 }
117 
118 void SBTypeSynthetic::SetOptions(uint32_t value) {
119   LLDB_RECORD_METHOD(void, SBTypeSynthetic, SetOptions, (uint32_t), value);
120 
121   if (!CopyOnWrite_Impl())
122     return;
123   m_opaque_sp->SetOptions(value);
124 }
125 
126 bool SBTypeSynthetic::GetDescription(lldb::SBStream &description,
127                                      lldb::DescriptionLevel description_level) {
128   LLDB_RECORD_METHOD(bool, SBTypeSynthetic, GetDescription,
129                      (lldb::SBStream &, lldb::DescriptionLevel), description,
130                      description_level);
131 
132   if (m_opaque_sp) {
133     description.Printf("%s\n", m_opaque_sp->GetDescription().c_str());
134     return true;
135   }
136   return false;
137 }
138 
139 lldb::SBTypeSynthetic &SBTypeSynthetic::
140 operator=(const lldb::SBTypeSynthetic &rhs) {
141   LLDB_RECORD_METHOD(lldb::SBTypeSynthetic &,
142                      SBTypeSynthetic, operator=,(const lldb::SBTypeSynthetic &),
143                      rhs);
144 
145   if (this != &rhs) {
146     m_opaque_sp = rhs.m_opaque_sp;
147   }
148   return *this;
149 }
150 
151 bool SBTypeSynthetic::operator==(lldb::SBTypeSynthetic &rhs) {
152   LLDB_RECORD_METHOD(
153       bool, SBTypeSynthetic, operator==,(lldb::SBTypeSynthetic &), rhs);
154 
155   if (!IsValid())
156     return !rhs.IsValid();
157   return m_opaque_sp == rhs.m_opaque_sp;
158 }
159 
160 bool SBTypeSynthetic::IsEqualTo(lldb::SBTypeSynthetic &rhs) {
161   LLDB_RECORD_METHOD(bool, SBTypeSynthetic, IsEqualTo,
162                      (lldb::SBTypeSynthetic &), rhs);
163 
164   if (!IsValid())
165     return !rhs.IsValid();
166 
167   if (m_opaque_sp->IsScripted() != rhs.m_opaque_sp->IsScripted())
168     return false;
169 
170   if (IsClassCode() != rhs.IsClassCode())
171     return false;
172 
173   if (strcmp(GetData(), rhs.GetData()))
174     return false;
175 
176   return GetOptions() == rhs.GetOptions();
177 }
178 
179 bool SBTypeSynthetic::operator!=(lldb::SBTypeSynthetic &rhs) {
180   LLDB_RECORD_METHOD(
181       bool, SBTypeSynthetic, operator!=,(lldb::SBTypeSynthetic &), rhs);
182 
183   if (!IsValid())
184     return !rhs.IsValid();
185   return m_opaque_sp != rhs.m_opaque_sp;
186 }
187 
188 lldb::ScriptedSyntheticChildrenSP SBTypeSynthetic::GetSP() {
189   return m_opaque_sp;
190 }
191 
192 void SBTypeSynthetic::SetSP(
193     const lldb::ScriptedSyntheticChildrenSP &TypeSynthetic_impl_sp) {
194   m_opaque_sp = TypeSynthetic_impl_sp;
195 }
196 
197 SBTypeSynthetic::SBTypeSynthetic(
198     const lldb::ScriptedSyntheticChildrenSP &TypeSynthetic_impl_sp)
199     : m_opaque_sp(TypeSynthetic_impl_sp) {}
200 
201 bool SBTypeSynthetic::CopyOnWrite_Impl() {
202   if (!IsValid())
203     return false;
204   if (m_opaque_sp.unique())
205     return true;
206 
207   ScriptedSyntheticChildrenSP new_sp(new ScriptedSyntheticChildren(
208       m_opaque_sp->GetOptions(), m_opaque_sp->GetPythonClassName(),
209       m_opaque_sp->GetPythonCode()));
210 
211   SetSP(new_sp);
212 
213   return true;
214 }
215 
216 #endif // LLDB_DISABLE_PYTHON
217