]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/llvm/tools/lldb/source/API/SBTypeFormat.cpp
Merge llvm, clang, compiler-rt, libc++, libunwind, lld, lldb and openmp
[FreeBSD/FreeBSD.git] / contrib / llvm / tools / lldb / source / API / SBTypeFormat.cpp
1 //===-- SBTypeFormat.cpp ------------------------------------------*- C++
2 //-*-===//
3 //
4 //                     The LLVM Compiler Infrastructure
5 //
6 // This file is distributed under the University of Illinois Open Source
7 // License. See LICENSE.TXT for details.
8 //
9 //===----------------------------------------------------------------------===//
10
11 #include "lldb/API/SBTypeFormat.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
22 SBTypeFormat::SBTypeFormat(lldb::Format format, uint32_t options)
23     : m_opaque_sp(
24           TypeFormatImplSP(new TypeFormatImpl_Format(format, options))) {}
25
26 SBTypeFormat::SBTypeFormat(const char *type, uint32_t options)
27     : m_opaque_sp(TypeFormatImplSP(new TypeFormatImpl_EnumType(
28           ConstString(type ? type : ""), options))) {}
29
30 SBTypeFormat::SBTypeFormat(const lldb::SBTypeFormat &rhs)
31     : m_opaque_sp(rhs.m_opaque_sp) {}
32
33 SBTypeFormat::~SBTypeFormat() {}
34
35 bool SBTypeFormat::IsValid() const { return m_opaque_sp.get() != NULL; }
36
37 lldb::Format SBTypeFormat::GetFormat() {
38   if (IsValid() && m_opaque_sp->GetType() == TypeFormatImpl::Type::eTypeFormat)
39     return ((TypeFormatImpl_Format *)m_opaque_sp.get())->GetFormat();
40   return lldb::eFormatInvalid;
41 }
42
43 const char *SBTypeFormat::GetTypeName() {
44   if (IsValid() && m_opaque_sp->GetType() == TypeFormatImpl::Type::eTypeEnum)
45     return ((TypeFormatImpl_EnumType *)m_opaque_sp.get())
46         ->GetTypeName()
47         .AsCString("");
48   return "";
49 }
50
51 uint32_t SBTypeFormat::GetOptions() {
52   if (IsValid())
53     return m_opaque_sp->GetOptions();
54   return 0;
55 }
56
57 void SBTypeFormat::SetFormat(lldb::Format fmt) {
58   if (CopyOnWrite_Impl(Type::eTypeFormat))
59     ((TypeFormatImpl_Format *)m_opaque_sp.get())->SetFormat(fmt);
60 }
61
62 void SBTypeFormat::SetTypeName(const char *type) {
63   if (CopyOnWrite_Impl(Type::eTypeEnum))
64     ((TypeFormatImpl_EnumType *)m_opaque_sp.get())
65         ->SetTypeName(ConstString(type ? type : ""));
66 }
67
68 void SBTypeFormat::SetOptions(uint32_t value) {
69   if (CopyOnWrite_Impl(Type::eTypeKeepSame))
70     m_opaque_sp->SetOptions(value);
71 }
72
73 bool SBTypeFormat::GetDescription(lldb::SBStream &description,
74                                   lldb::DescriptionLevel description_level) {
75   if (!IsValid())
76     return false;
77   else {
78     description.Printf("%s\n", m_opaque_sp->GetDescription().c_str());
79     return true;
80   }
81 }
82
83 lldb::SBTypeFormat &SBTypeFormat::operator=(const lldb::SBTypeFormat &rhs) {
84   if (this != &rhs) {
85     m_opaque_sp = rhs.m_opaque_sp;
86   }
87   return *this;
88 }
89
90 bool SBTypeFormat::operator==(lldb::SBTypeFormat &rhs) {
91   if (!IsValid())
92     return !rhs.IsValid();
93   return m_opaque_sp == rhs.m_opaque_sp;
94 }
95
96 bool SBTypeFormat::IsEqualTo(lldb::SBTypeFormat &rhs) {
97   if (!IsValid())
98     return !rhs.IsValid();
99
100   if (GetFormat() == rhs.GetFormat())
101     return GetOptions() == rhs.GetOptions();
102   else
103     return false;
104 }
105
106 bool SBTypeFormat::operator!=(lldb::SBTypeFormat &rhs) {
107   if (!IsValid())
108     return !rhs.IsValid();
109   return m_opaque_sp != rhs.m_opaque_sp;
110 }
111
112 lldb::TypeFormatImplSP SBTypeFormat::GetSP() { return m_opaque_sp; }
113
114 void SBTypeFormat::SetSP(const lldb::TypeFormatImplSP &typeformat_impl_sp) {
115   m_opaque_sp = typeformat_impl_sp;
116 }
117
118 SBTypeFormat::SBTypeFormat(const lldb::TypeFormatImplSP &typeformat_impl_sp)
119     : m_opaque_sp(typeformat_impl_sp) {}
120
121 bool SBTypeFormat::CopyOnWrite_Impl(Type type) {
122   if (!IsValid())
123     return false;
124
125   if (m_opaque_sp.unique() &&
126       ((type == Type::eTypeKeepSame) ||
127        (type == Type::eTypeFormat &&
128         m_opaque_sp->GetType() == TypeFormatImpl::Type::eTypeFormat) ||
129        (type == Type::eTypeEnum &&
130         m_opaque_sp->GetType() == TypeFormatImpl::Type::eTypeEnum)))
131     return true;
132
133   if (type == Type::eTypeKeepSame) {
134     if (m_opaque_sp->GetType() == TypeFormatImpl::Type::eTypeFormat)
135       type = Type::eTypeFormat;
136     else
137       type = Type::eTypeEnum;
138   }
139
140   if (type == Type::eTypeFormat)
141     SetSP(
142         TypeFormatImplSP(new TypeFormatImpl_Format(GetFormat(), GetOptions())));
143   else
144     SetSP(TypeFormatImplSP(
145         new TypeFormatImpl_EnumType(ConstString(GetTypeName()), GetOptions())));
146
147   return true;
148 }