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 
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 #ifndef LLDB_DISABLE_PYTHON
20 
21 SBTypeSynthetic::SBTypeSynthetic() : m_opaque_sp() {}
22 
23 SBTypeSynthetic SBTypeSynthetic::CreateWithClassName(const char *data,
24                                                      uint32_t options) {
25   if (!data || data[0] == 0)
26     return SBTypeSynthetic();
27   return SBTypeSynthetic(ScriptedSyntheticChildrenSP(
28       new ScriptedSyntheticChildren(options, data, "")));
29 }
30 
31 SBTypeSynthetic SBTypeSynthetic::CreateWithScriptCode(const char *data,
32                                                       uint32_t options) {
33   if (!data || data[0] == 0)
34     return SBTypeSynthetic();
35   return SBTypeSynthetic(ScriptedSyntheticChildrenSP(
36       new ScriptedSyntheticChildren(options, "", data)));
37 }
38 
39 SBTypeSynthetic::SBTypeSynthetic(const lldb::SBTypeSynthetic &rhs)
40     : m_opaque_sp(rhs.m_opaque_sp) {}
41 
42 SBTypeSynthetic::~SBTypeSynthetic() {}
43 
44 bool SBTypeSynthetic::IsValid() const { return m_opaque_sp.get() != NULL; }
45 
46 bool SBTypeSynthetic::IsClassCode() {
47   if (!IsValid())
48     return false;
49   const char *code = m_opaque_sp->GetPythonCode();
50   return (code && *code);
51 }
52 
53 bool SBTypeSynthetic::IsClassName() {
54   if (!IsValid())
55     return false;
56   return !IsClassCode();
57 }
58 
59 const char *SBTypeSynthetic::GetData() {
60   if (!IsValid())
61     return NULL;
62   if (IsClassCode())
63     return m_opaque_sp->GetPythonCode();
64   else
65     return m_opaque_sp->GetPythonClassName();
66 }
67 
68 void SBTypeSynthetic::SetClassName(const char *data) {
69   if (IsValid() && data && *data)
70     m_opaque_sp->SetPythonClassName(data);
71 }
72 
73 void SBTypeSynthetic::SetClassCode(const char *data) {
74   if (IsValid() && data && *data)
75     m_opaque_sp->SetPythonCode(data);
76 }
77 
78 uint32_t SBTypeSynthetic::GetOptions() {
79   if (!IsValid())
80     return lldb::eTypeOptionNone;
81   return m_opaque_sp->GetOptions();
82 }
83 
84 void SBTypeSynthetic::SetOptions(uint32_t value) {
85   if (!CopyOnWrite_Impl())
86     return;
87   m_opaque_sp->SetOptions(value);
88 }
89 
90 bool SBTypeSynthetic::GetDescription(lldb::SBStream &description,
91                                      lldb::DescriptionLevel description_level) {
92   if (m_opaque_sp) {
93     description.Printf("%s\n", m_opaque_sp->GetDescription().c_str());
94     return true;
95   }
96   return false;
97 }
98 
99 lldb::SBTypeSynthetic &SBTypeSynthetic::
100 operator=(const lldb::SBTypeSynthetic &rhs) {
101   if (this != &rhs) {
102     m_opaque_sp = rhs.m_opaque_sp;
103   }
104   return *this;
105 }
106 
107 bool SBTypeSynthetic::operator==(lldb::SBTypeSynthetic &rhs) {
108   if (!IsValid())
109     return !rhs.IsValid();
110   return m_opaque_sp == rhs.m_opaque_sp;
111 }
112 
113 bool SBTypeSynthetic::IsEqualTo(lldb::SBTypeSynthetic &rhs) {
114   if (!IsValid())
115     return !rhs.IsValid();
116 
117   if (m_opaque_sp->IsScripted() != rhs.m_opaque_sp->IsScripted())
118     return false;
119 
120   if (IsClassCode() != rhs.IsClassCode())
121     return false;
122 
123   if (strcmp(GetData(), rhs.GetData()))
124     return false;
125 
126   return GetOptions() == rhs.GetOptions();
127 }
128 
129 bool SBTypeSynthetic::operator!=(lldb::SBTypeSynthetic &rhs) {
130   if (!IsValid())
131     return !rhs.IsValid();
132   return m_opaque_sp != rhs.m_opaque_sp;
133 }
134 
135 lldb::ScriptedSyntheticChildrenSP SBTypeSynthetic::GetSP() {
136   return m_opaque_sp;
137 }
138 
139 void SBTypeSynthetic::SetSP(
140     const lldb::ScriptedSyntheticChildrenSP &TypeSynthetic_impl_sp) {
141   m_opaque_sp = TypeSynthetic_impl_sp;
142 }
143 
144 SBTypeSynthetic::SBTypeSynthetic(
145     const lldb::ScriptedSyntheticChildrenSP &TypeSynthetic_impl_sp)
146     : m_opaque_sp(TypeSynthetic_impl_sp) {}
147 
148 bool SBTypeSynthetic::CopyOnWrite_Impl() {
149   if (!IsValid())
150     return false;
151   if (m_opaque_sp.unique())
152     return true;
153 
154   ScriptedSyntheticChildrenSP new_sp(new ScriptedSyntheticChildren(
155       m_opaque_sp->GetOptions(), m_opaque_sp->GetPythonClassName(),
156       m_opaque_sp->GetPythonCode()));
157 
158   SetSP(new_sp);
159 
160   return true;
161 }
162 
163 #endif // LLDB_DISABLE_PYTHON
164