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 "lldb/Utility/Instrumentation.h"
12 #include "lldb/API/SBStream.h"
14 #include "lldb/DataFormatters/DataVisualization.h"
17 using namespace lldb_private
;
19 SBTypeFormat::SBTypeFormat() { LLDB_INSTRUMENT_VA(this); }
21 SBTypeFormat::SBTypeFormat(lldb::Format format
, uint32_t options
)
23 TypeFormatImplSP(new TypeFormatImpl_Format(format
, options
))) {
24 LLDB_INSTRUMENT_VA(this, format
, options
);
27 SBTypeFormat::SBTypeFormat(const char *type
, uint32_t options
)
28 : m_opaque_sp(TypeFormatImplSP(new TypeFormatImpl_EnumType(
29 ConstString(type
? type
: ""), options
))) {
30 LLDB_INSTRUMENT_VA(this, type
, options
);
33 SBTypeFormat::SBTypeFormat(const lldb::SBTypeFormat
&rhs
)
34 : m_opaque_sp(rhs
.m_opaque_sp
) {
35 LLDB_INSTRUMENT_VA(this, rhs
);
38 SBTypeFormat::~SBTypeFormat() = default;
40 bool SBTypeFormat::IsValid() const {
41 LLDB_INSTRUMENT_VA(this);
42 return this->operator bool();
44 SBTypeFormat::operator bool() const {
45 LLDB_INSTRUMENT_VA(this);
47 return m_opaque_sp
.get() != nullptr;
50 lldb::Format
SBTypeFormat::GetFormat() {
51 LLDB_INSTRUMENT_VA(this);
53 if (IsValid() && m_opaque_sp
->GetType() == TypeFormatImpl::Type::eTypeFormat
)
54 return ((TypeFormatImpl_Format
*)m_opaque_sp
.get())->GetFormat();
55 return lldb::eFormatInvalid
;
58 const char *SBTypeFormat::GetTypeName() {
59 LLDB_INSTRUMENT_VA(this);
61 if (IsValid() && m_opaque_sp
->GetType() == TypeFormatImpl::Type::eTypeEnum
)
62 return ((TypeFormatImpl_EnumType
*)m_opaque_sp
.get())
68 uint32_t SBTypeFormat::GetOptions() {
69 LLDB_INSTRUMENT_VA(this);
72 return m_opaque_sp
->GetOptions();
76 void SBTypeFormat::SetFormat(lldb::Format fmt
) {
77 LLDB_INSTRUMENT_VA(this, fmt
);
79 if (CopyOnWrite_Impl(Type::eTypeFormat
))
80 ((TypeFormatImpl_Format
*)m_opaque_sp
.get())->SetFormat(fmt
);
83 void SBTypeFormat::SetTypeName(const char *type
) {
84 LLDB_INSTRUMENT_VA(this, type
);
86 if (CopyOnWrite_Impl(Type::eTypeEnum
))
87 ((TypeFormatImpl_EnumType
*)m_opaque_sp
.get())
88 ->SetTypeName(ConstString(type
? type
: ""));
91 void SBTypeFormat::SetOptions(uint32_t value
) {
92 LLDB_INSTRUMENT_VA(this, value
);
94 if (CopyOnWrite_Impl(Type::eTypeKeepSame
))
95 m_opaque_sp
->SetOptions(value
);
98 bool SBTypeFormat::GetDescription(lldb::SBStream
&description
,
99 lldb::DescriptionLevel description_level
) {
100 LLDB_INSTRUMENT_VA(this, description
, description_level
);
105 description
.Printf("%s\n", m_opaque_sp
->GetDescription().c_str());
110 lldb::SBTypeFormat
&SBTypeFormat::operator=(const lldb::SBTypeFormat
&rhs
) {
111 LLDB_INSTRUMENT_VA(this, rhs
);
114 m_opaque_sp
= rhs
.m_opaque_sp
;
119 bool SBTypeFormat::operator==(lldb::SBTypeFormat
&rhs
) {
120 LLDB_INSTRUMENT_VA(this, rhs
);
123 return !rhs
.IsValid();
124 return m_opaque_sp
== rhs
.m_opaque_sp
;
127 bool SBTypeFormat::IsEqualTo(lldb::SBTypeFormat
&rhs
) {
128 LLDB_INSTRUMENT_VA(this, rhs
);
131 return !rhs
.IsValid();
133 if (GetFormat() == rhs
.GetFormat())
134 return GetOptions() == rhs
.GetOptions();
139 bool SBTypeFormat::operator!=(lldb::SBTypeFormat
&rhs
) {
140 LLDB_INSTRUMENT_VA(this, rhs
);
143 return !rhs
.IsValid();
144 return m_opaque_sp
!= rhs
.m_opaque_sp
;
147 lldb::TypeFormatImplSP
SBTypeFormat::GetSP() { return m_opaque_sp
; }
149 void SBTypeFormat::SetSP(const lldb::TypeFormatImplSP
&typeformat_impl_sp
) {
150 m_opaque_sp
= typeformat_impl_sp
;
153 SBTypeFormat::SBTypeFormat(const lldb::TypeFormatImplSP
&typeformat_impl_sp
)
154 : m_opaque_sp(typeformat_impl_sp
) {}
156 bool SBTypeFormat::CopyOnWrite_Impl(Type type
) {
160 if (m_opaque_sp
.unique() &&
161 ((type
== Type::eTypeKeepSame
) ||
162 (type
== Type::eTypeFormat
&&
163 m_opaque_sp
->GetType() == TypeFormatImpl::Type::eTypeFormat
) ||
164 (type
== Type::eTypeEnum
&&
165 m_opaque_sp
->GetType() == TypeFormatImpl::Type::eTypeEnum
)))
168 if (type
== Type::eTypeKeepSame
) {
169 if (m_opaque_sp
->GetType() == TypeFormatImpl::Type::eTypeFormat
)
170 type
= Type::eTypeFormat
;
172 type
= Type::eTypeEnum
;
175 if (type
== Type::eTypeFormat
)
177 TypeFormatImplSP(new TypeFormatImpl_Format(GetFormat(), GetOptions())));
179 SetSP(TypeFormatImplSP(
180 new TypeFormatImpl_EnumType(ConstString(GetTypeName()), GetOptions())));