1 //===-- SBTypeFormat.cpp --------------------------------------------------===//
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
7 //===----------------------------------------------------------------------===//
9 #include "lldb/API/SBTypeFormat.h"
10 #include "SBReproducerPrivate.h"
12 #include "lldb/API/SBStream.h"
14 #include "lldb/DataFormatters/DataVisualization.h"
17 using namespace lldb_private;
19 SBTypeFormat::SBTypeFormat() : m_opaque_sp() {
20 LLDB_RECORD_CONSTRUCTOR_NO_ARGS(SBTypeFormat);
23 SBTypeFormat::SBTypeFormat(lldb::Format format, uint32_t options)
25 TypeFormatImplSP(new TypeFormatImpl_Format(format, options))) {
26 LLDB_RECORD_CONSTRUCTOR(SBTypeFormat, (lldb::Format, uint32_t), format,
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,
37 SBTypeFormat::SBTypeFormat(const lldb::SBTypeFormat &rhs)
38 : m_opaque_sp(rhs.m_opaque_sp) {
39 LLDB_RECORD_CONSTRUCTOR(SBTypeFormat, (const lldb::SBTypeFormat &), rhs);
42 SBTypeFormat::~SBTypeFormat() = default;
44 bool SBTypeFormat::IsValid() const {
45 LLDB_RECORD_METHOD_CONST_NO_ARGS(bool, SBTypeFormat, IsValid);
46 return this->operator bool();
48 SBTypeFormat::operator bool() const {
49 LLDB_RECORD_METHOD_CONST_NO_ARGS(bool, SBTypeFormat, operator bool);
51 return m_opaque_sp.get() != nullptr;
54 lldb::Format SBTypeFormat::GetFormat() {
55 LLDB_RECORD_METHOD_NO_ARGS(lldb::Format, SBTypeFormat, GetFormat);
57 if (IsValid() && m_opaque_sp->GetType() == TypeFormatImpl::Type::eTypeFormat)
58 return ((TypeFormatImpl_Format *)m_opaque_sp.get())->GetFormat();
59 return lldb::eFormatInvalid;
62 const char *SBTypeFormat::GetTypeName() {
63 LLDB_RECORD_METHOD_NO_ARGS(const char *, SBTypeFormat, GetTypeName);
65 if (IsValid() && m_opaque_sp->GetType() == TypeFormatImpl::Type::eTypeEnum)
66 return ((TypeFormatImpl_EnumType *)m_opaque_sp.get())
72 uint32_t SBTypeFormat::GetOptions() {
73 LLDB_RECORD_METHOD_NO_ARGS(uint32_t, SBTypeFormat, GetOptions);
76 return m_opaque_sp->GetOptions();
80 void SBTypeFormat::SetFormat(lldb::Format fmt) {
81 LLDB_RECORD_METHOD(void, SBTypeFormat, SetFormat, (lldb::Format), fmt);
83 if (CopyOnWrite_Impl(Type::eTypeFormat))
84 ((TypeFormatImpl_Format *)m_opaque_sp.get())->SetFormat(fmt);
87 void SBTypeFormat::SetTypeName(const char *type) {
88 LLDB_RECORD_METHOD(void, SBTypeFormat, SetTypeName, (const char *), type);
90 if (CopyOnWrite_Impl(Type::eTypeEnum))
91 ((TypeFormatImpl_EnumType *)m_opaque_sp.get())
92 ->SetTypeName(ConstString(type ? type : ""));
95 void SBTypeFormat::SetOptions(uint32_t value) {
96 LLDB_RECORD_METHOD(void, SBTypeFormat, SetOptions, (uint32_t), value);
98 if (CopyOnWrite_Impl(Type::eTypeKeepSame))
99 m_opaque_sp->SetOptions(value);
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,
111 description.Printf("%s\n", m_opaque_sp->GetDescription().c_str());
116 lldb::SBTypeFormat &SBTypeFormat::operator=(const lldb::SBTypeFormat &rhs) {
117 LLDB_RECORD_METHOD(lldb::SBTypeFormat &,
118 SBTypeFormat, operator=,(const lldb::SBTypeFormat &), rhs);
121 m_opaque_sp = rhs.m_opaque_sp;
123 return LLDB_RECORD_RESULT(*this);
126 bool SBTypeFormat::operator==(lldb::SBTypeFormat &rhs) {
127 LLDB_RECORD_METHOD(bool, SBTypeFormat, operator==,(lldb::SBTypeFormat &),
131 return !rhs.IsValid();
132 return m_opaque_sp == rhs.m_opaque_sp;
135 bool SBTypeFormat::IsEqualTo(lldb::SBTypeFormat &rhs) {
136 LLDB_RECORD_METHOD(bool, SBTypeFormat, IsEqualTo, (lldb::SBTypeFormat &),
140 return !rhs.IsValid();
142 if (GetFormat() == rhs.GetFormat())
143 return GetOptions() == rhs.GetOptions();
148 bool SBTypeFormat::operator!=(lldb::SBTypeFormat &rhs) {
149 LLDB_RECORD_METHOD(bool, SBTypeFormat, operator!=,(lldb::SBTypeFormat &),
153 return !rhs.IsValid();
154 return m_opaque_sp != rhs.m_opaque_sp;
157 lldb::TypeFormatImplSP SBTypeFormat::GetSP() { return m_opaque_sp; }
159 void SBTypeFormat::SetSP(const lldb::TypeFormatImplSP &typeformat_impl_sp) {
160 m_opaque_sp = typeformat_impl_sp;
163 SBTypeFormat::SBTypeFormat(const lldb::TypeFormatImplSP &typeformat_impl_sp)
164 : m_opaque_sp(typeformat_impl_sp) {}
166 bool SBTypeFormat::CopyOnWrite_Impl(Type type) {
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)))
178 if (type == Type::eTypeKeepSame) {
179 if (m_opaque_sp->GetType() == TypeFormatImpl::Type::eTypeFormat)
180 type = Type::eTypeFormat;
182 type = Type::eTypeEnum;
185 if (type == Type::eTypeFormat)
187 TypeFormatImplSP(new TypeFormatImpl_Format(GetFormat(), GetOptions())));
189 SetSP(TypeFormatImplSP(
190 new TypeFormatImpl_EnumType(ConstString(GetTypeName()), GetOptions())));
195 namespace lldb_private {
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 &));