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