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 "lldb/Utility/Instrumentation.h"
11
12#include "lldb/API/SBStream.h"
13
14#include "lldb/DataFormatters/DataVisualization.h"
15
16using namespace lldb;
17using namespace lldb_private;
18
19SBTypeFormat::SBTypeFormat() { LLDB_INSTRUMENT_VA(this); }
20
21SBTypeFormat::SBTypeFormat(lldb::Format format, uint32_t options)
22    : m_opaque_sp(
23          TypeFormatImplSP(new TypeFormatImpl_Format(format, options))) {
24  LLDB_INSTRUMENT_VA(this, format, options);
25}
26
27SBTypeFormat::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);
31}
32
33SBTypeFormat::SBTypeFormat(const lldb::SBTypeFormat &rhs)
34    : m_opaque_sp(rhs.m_opaque_sp) {
35  LLDB_INSTRUMENT_VA(this, rhs);
36}
37
38SBTypeFormat::~SBTypeFormat() = default;
39
40bool SBTypeFormat::IsValid() const {
41  LLDB_INSTRUMENT_VA(this);
42  return this->operator bool();
43}
44SBTypeFormat::operator bool() const {
45  LLDB_INSTRUMENT_VA(this);
46
47  return m_opaque_sp.get() != nullptr;
48}
49
50lldb::Format SBTypeFormat::GetFormat() {
51  LLDB_INSTRUMENT_VA(this);
52
53  if (IsValid() && m_opaque_sp->GetType() == TypeFormatImpl::Type::eTypeFormat)
54    return ((TypeFormatImpl_Format *)m_opaque_sp.get())->GetFormat();
55  return lldb::eFormatInvalid;
56}
57
58const char *SBTypeFormat::GetTypeName() {
59  LLDB_INSTRUMENT_VA(this);
60
61  if (IsValid() && m_opaque_sp->GetType() == TypeFormatImpl::Type::eTypeEnum)
62    return ((TypeFormatImpl_EnumType *)m_opaque_sp.get())
63        ->GetTypeName()
64        .AsCString("");
65  return "";
66}
67
68uint32_t SBTypeFormat::GetOptions() {
69  LLDB_INSTRUMENT_VA(this);
70
71  if (IsValid())
72    return m_opaque_sp->GetOptions();
73  return 0;
74}
75
76void SBTypeFormat::SetFormat(lldb::Format fmt) {
77  LLDB_INSTRUMENT_VA(this, fmt);
78
79  if (CopyOnWrite_Impl(Type::eTypeFormat))
80    ((TypeFormatImpl_Format *)m_opaque_sp.get())->SetFormat(fmt);
81}
82
83void SBTypeFormat::SetTypeName(const char *type) {
84  LLDB_INSTRUMENT_VA(this, type);
85
86  if (CopyOnWrite_Impl(Type::eTypeEnum))
87    ((TypeFormatImpl_EnumType *)m_opaque_sp.get())
88        ->SetTypeName(ConstString(type ? type : ""));
89}
90
91void SBTypeFormat::SetOptions(uint32_t value) {
92  LLDB_INSTRUMENT_VA(this, value);
93
94  if (CopyOnWrite_Impl(Type::eTypeKeepSame))
95    m_opaque_sp->SetOptions(value);
96}
97
98bool SBTypeFormat::GetDescription(lldb::SBStream &description,
99                                  lldb::DescriptionLevel description_level) {
100  LLDB_INSTRUMENT_VA(this, description, description_level);
101
102  if (!IsValid())
103    return false;
104  else {
105    description.Printf("%s\n", m_opaque_sp->GetDescription().c_str());
106    return true;
107  }
108}
109
110lldb::SBTypeFormat &SBTypeFormat::operator=(const lldb::SBTypeFormat &rhs) {
111  LLDB_INSTRUMENT_VA(this, rhs);
112
113  if (this != &rhs) {
114    m_opaque_sp = rhs.m_opaque_sp;
115  }
116  return *this;
117}
118
119bool SBTypeFormat::operator==(lldb::SBTypeFormat &rhs) {
120  LLDB_INSTRUMENT_VA(this, rhs);
121
122  if (!IsValid())
123    return !rhs.IsValid();
124  return m_opaque_sp == rhs.m_opaque_sp;
125}
126
127bool SBTypeFormat::IsEqualTo(lldb::SBTypeFormat &rhs) {
128  LLDB_INSTRUMENT_VA(this, rhs);
129
130  if (!IsValid())
131    return !rhs.IsValid();
132
133  if (GetFormat() == rhs.GetFormat())
134    return GetOptions() == rhs.GetOptions();
135  else
136    return false;
137}
138
139bool SBTypeFormat::operator!=(lldb::SBTypeFormat &rhs) {
140  LLDB_INSTRUMENT_VA(this, rhs);
141
142  if (!IsValid())
143    return !rhs.IsValid();
144  return m_opaque_sp != rhs.m_opaque_sp;
145}
146
147lldb::TypeFormatImplSP SBTypeFormat::GetSP() { return m_opaque_sp; }
148
149void SBTypeFormat::SetSP(const lldb::TypeFormatImplSP &typeformat_impl_sp) {
150  m_opaque_sp = typeformat_impl_sp;
151}
152
153SBTypeFormat::SBTypeFormat(const lldb::TypeFormatImplSP &typeformat_impl_sp)
154    : m_opaque_sp(typeformat_impl_sp) {}
155
156bool SBTypeFormat::CopyOnWrite_Impl(Type type) {
157  if (!IsValid())
158    return false;
159
160  if (m_opaque_sp.use_count() == 1 &&
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)))
166    return true;
167
168  if (type == Type::eTypeKeepSame) {
169    if (m_opaque_sp->GetType() == TypeFormatImpl::Type::eTypeFormat)
170      type = Type::eTypeFormat;
171    else
172      type = Type::eTypeEnum;
173  }
174
175  if (type == Type::eTypeFormat)
176    SetSP(
177        TypeFormatImplSP(new TypeFormatImpl_Format(GetFormat(), GetOptions())));
178  else
179    SetSP(TypeFormatImplSP(
180        new TypeFormatImpl_EnumType(ConstString(GetTypeName()), GetOptions())));
181
182  return true;
183}
184