1 //===-- SBTypeFormat.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/SBTypeFormat.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
SBTypeFormat()19 SBTypeFormat::SBTypeFormat() : m_opaque_sp() {
20 LLDB_RECORD_CONSTRUCTOR_NO_ARGS(SBTypeFormat);
21 }
22
SBTypeFormat(lldb::Format format,uint32_t options)23 SBTypeFormat::SBTypeFormat(lldb::Format format, uint32_t options)
24 : m_opaque_sp(
25 TypeFormatImplSP(new TypeFormatImpl_Format(format, options))) {
26 LLDB_RECORD_CONSTRUCTOR(SBTypeFormat, (lldb::Format, uint32_t), format,
27 options);
28 }
29
SBTypeFormat(const char * type,uint32_t options)30 SBTypeFormat::SBTypeFormat(const char *type, uint32_t options)
31 : m_opaque_sp(TypeFormatImplSP(new TypeFormatImpl_EnumType(
32 ConstString(type ? type : ""), options))) {
33 LLDB_RECORD_CONSTRUCTOR(SBTypeFormat, (const char *, uint32_t), type,
34 options);
35 }
36
SBTypeFormat(const lldb::SBTypeFormat & rhs)37 SBTypeFormat::SBTypeFormat(const lldb::SBTypeFormat &rhs)
38 : m_opaque_sp(rhs.m_opaque_sp) {
39 LLDB_RECORD_CONSTRUCTOR(SBTypeFormat, (const lldb::SBTypeFormat &), rhs);
40 }
41
42 SBTypeFormat::~SBTypeFormat() = default;
43
IsValid() const44 bool SBTypeFormat::IsValid() const {
45 LLDB_RECORD_METHOD_CONST_NO_ARGS(bool, SBTypeFormat, IsValid);
46 return this->operator bool();
47 }
operator bool() const48 SBTypeFormat::operator bool() const {
49 LLDB_RECORD_METHOD_CONST_NO_ARGS(bool, SBTypeFormat, operator bool);
50
51 return m_opaque_sp.get() != nullptr;
52 }
53
GetFormat()54 lldb::Format SBTypeFormat::GetFormat() {
55 LLDB_RECORD_METHOD_NO_ARGS(lldb::Format, SBTypeFormat, GetFormat);
56
57 if (IsValid() && m_opaque_sp->GetType() == TypeFormatImpl::Type::eTypeFormat)
58 return ((TypeFormatImpl_Format *)m_opaque_sp.get())->GetFormat();
59 return lldb::eFormatInvalid;
60 }
61
GetTypeName()62 const char *SBTypeFormat::GetTypeName() {
63 LLDB_RECORD_METHOD_NO_ARGS(const char *, SBTypeFormat, GetTypeName);
64
65 if (IsValid() && m_opaque_sp->GetType() == TypeFormatImpl::Type::eTypeEnum)
66 return ((TypeFormatImpl_EnumType *)m_opaque_sp.get())
67 ->GetTypeName()
68 .AsCString("");
69 return "";
70 }
71
GetOptions()72 uint32_t SBTypeFormat::GetOptions() {
73 LLDB_RECORD_METHOD_NO_ARGS(uint32_t, SBTypeFormat, GetOptions);
74
75 if (IsValid())
76 return m_opaque_sp->GetOptions();
77 return 0;
78 }
79
SetFormat(lldb::Format fmt)80 void SBTypeFormat::SetFormat(lldb::Format fmt) {
81 LLDB_RECORD_METHOD(void, SBTypeFormat, SetFormat, (lldb::Format), fmt);
82
83 if (CopyOnWrite_Impl(Type::eTypeFormat))
84 ((TypeFormatImpl_Format *)m_opaque_sp.get())->SetFormat(fmt);
85 }
86
SetTypeName(const char * type)87 void SBTypeFormat::SetTypeName(const char *type) {
88 LLDB_RECORD_METHOD(void, SBTypeFormat, SetTypeName, (const char *), type);
89
90 if (CopyOnWrite_Impl(Type::eTypeEnum))
91 ((TypeFormatImpl_EnumType *)m_opaque_sp.get())
92 ->SetTypeName(ConstString(type ? type : ""));
93 }
94
SetOptions(uint32_t value)95 void SBTypeFormat::SetOptions(uint32_t value) {
96 LLDB_RECORD_METHOD(void, SBTypeFormat, SetOptions, (uint32_t), value);
97
98 if (CopyOnWrite_Impl(Type::eTypeKeepSame))
99 m_opaque_sp->SetOptions(value);
100 }
101
GetDescription(lldb::SBStream & description,lldb::DescriptionLevel description_level)102 bool SBTypeFormat::GetDescription(lldb::SBStream &description,
103 lldb::DescriptionLevel description_level) {
104 LLDB_RECORD_METHOD(bool, SBTypeFormat, GetDescription,
105 (lldb::SBStream &, lldb::DescriptionLevel), description,
106 description_level);
107
108 if (!IsValid())
109 return false;
110 else {
111 description.Printf("%s\n", m_opaque_sp->GetDescription().c_str());
112 return true;
113 }
114 }
115
operator =(const lldb::SBTypeFormat & rhs)116 lldb::SBTypeFormat &SBTypeFormat::operator=(const lldb::SBTypeFormat &rhs) {
117 LLDB_RECORD_METHOD(lldb::SBTypeFormat &,
118 SBTypeFormat, operator=,(const lldb::SBTypeFormat &), rhs);
119
120 if (this != &rhs) {
121 m_opaque_sp = rhs.m_opaque_sp;
122 }
123 return LLDB_RECORD_RESULT(*this);
124 }
125
operator ==(lldb::SBTypeFormat & rhs)126 bool SBTypeFormat::operator==(lldb::SBTypeFormat &rhs) {
127 LLDB_RECORD_METHOD(bool, SBTypeFormat, operator==,(lldb::SBTypeFormat &),
128 rhs);
129
130 if (!IsValid())
131 return !rhs.IsValid();
132 return m_opaque_sp == rhs.m_opaque_sp;
133 }
134
IsEqualTo(lldb::SBTypeFormat & rhs)135 bool SBTypeFormat::IsEqualTo(lldb::SBTypeFormat &rhs) {
136 LLDB_RECORD_METHOD(bool, SBTypeFormat, IsEqualTo, (lldb::SBTypeFormat &),
137 rhs);
138
139 if (!IsValid())
140 return !rhs.IsValid();
141
142 if (GetFormat() == rhs.GetFormat())
143 return GetOptions() == rhs.GetOptions();
144 else
145 return false;
146 }
147
operator !=(lldb::SBTypeFormat & rhs)148 bool SBTypeFormat::operator!=(lldb::SBTypeFormat &rhs) {
149 LLDB_RECORD_METHOD(bool, SBTypeFormat, operator!=,(lldb::SBTypeFormat &),
150 rhs);
151
152 if (!IsValid())
153 return !rhs.IsValid();
154 return m_opaque_sp != rhs.m_opaque_sp;
155 }
156
GetSP()157 lldb::TypeFormatImplSP SBTypeFormat::GetSP() { return m_opaque_sp; }
158
SetSP(const lldb::TypeFormatImplSP & typeformat_impl_sp)159 void SBTypeFormat::SetSP(const lldb::TypeFormatImplSP &typeformat_impl_sp) {
160 m_opaque_sp = typeformat_impl_sp;
161 }
162
SBTypeFormat(const lldb::TypeFormatImplSP & typeformat_impl_sp)163 SBTypeFormat::SBTypeFormat(const lldb::TypeFormatImplSP &typeformat_impl_sp)
164 : m_opaque_sp(typeformat_impl_sp) {}
165
CopyOnWrite_Impl(Type type)166 bool SBTypeFormat::CopyOnWrite_Impl(Type type) {
167 if (!IsValid())
168 return false;
169
170 if (m_opaque_sp.unique() &&
171 ((type == Type::eTypeKeepSame) ||
172 (type == Type::eTypeFormat &&
173 m_opaque_sp->GetType() == TypeFormatImpl::Type::eTypeFormat) ||
174 (type == Type::eTypeEnum &&
175 m_opaque_sp->GetType() == TypeFormatImpl::Type::eTypeEnum)))
176 return true;
177
178 if (type == Type::eTypeKeepSame) {
179 if (m_opaque_sp->GetType() == TypeFormatImpl::Type::eTypeFormat)
180 type = Type::eTypeFormat;
181 else
182 type = Type::eTypeEnum;
183 }
184
185 if (type == Type::eTypeFormat)
186 SetSP(
187 TypeFormatImplSP(new TypeFormatImpl_Format(GetFormat(), GetOptions())));
188 else
189 SetSP(TypeFormatImplSP(
190 new TypeFormatImpl_EnumType(ConstString(GetTypeName()), GetOptions())));
191
192 return true;
193 }
194
195 namespace lldb_private {
196 namespace repro {
197
198 template <>
RegisterMethods(Registry & R)199 void RegisterMethods<SBTypeFormat>(Registry &R) {
200 LLDB_REGISTER_CONSTRUCTOR(SBTypeFormat, ());
201 LLDB_REGISTER_CONSTRUCTOR(SBTypeFormat, (lldb::Format, uint32_t));
202 LLDB_REGISTER_CONSTRUCTOR(SBTypeFormat, (const char *, uint32_t));
203 LLDB_REGISTER_CONSTRUCTOR(SBTypeFormat, (const lldb::SBTypeFormat &));
204 LLDB_REGISTER_METHOD_CONST(bool, SBTypeFormat, IsValid, ());
205 LLDB_REGISTER_METHOD_CONST(bool, SBTypeFormat, operator bool, ());
206 LLDB_REGISTER_METHOD(lldb::Format, SBTypeFormat, GetFormat, ());
207 LLDB_REGISTER_METHOD(const char *, SBTypeFormat, GetTypeName, ());
208 LLDB_REGISTER_METHOD(uint32_t, SBTypeFormat, GetOptions, ());
209 LLDB_REGISTER_METHOD(void, SBTypeFormat, SetFormat, (lldb::Format));
210 LLDB_REGISTER_METHOD(void, SBTypeFormat, SetTypeName, (const char *));
211 LLDB_REGISTER_METHOD(void, SBTypeFormat, SetOptions, (uint32_t));
212 LLDB_REGISTER_METHOD(bool, SBTypeFormat, GetDescription,
213 (lldb::SBStream &, lldb::DescriptionLevel));
214 LLDB_REGISTER_METHOD(lldb::SBTypeFormat &,
215 SBTypeFormat, operator=,(const lldb::SBTypeFormat &));
216 LLDB_REGISTER_METHOD(bool, SBTypeFormat, operator==,(lldb::SBTypeFormat &));
217 LLDB_REGISTER_METHOD(bool, SBTypeFormat, IsEqualTo, (lldb::SBTypeFormat &));
218 LLDB_REGISTER_METHOD(bool, SBTypeFormat, operator!=,(lldb::SBTypeFormat &));
219 }
220
221 }
222 }
223