1 //===-- SBTypeNameSpecifier.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/SBTypeNameSpecifier.h"
10 #include "lldb/Utility/Instrumentation.h"
11 
12 #include "lldb/API/SBStream.h"
13 #include "lldb/API/SBType.h"
14 
15 #include "lldb/DataFormatters/DataVisualization.h"
16 
17 using namespace lldb;
18 using namespace lldb_private;
19 
20 SBTypeNameSpecifier::SBTypeNameSpecifier() { LLDB_INSTRUMENT_VA(this); }
21 
22 SBTypeNameSpecifier::SBTypeNameSpecifier(const char *name, bool is_regex)
23     : SBTypeNameSpecifier(name, is_regex ? eFormatterMatchRegex
24                                          : eFormatterMatchExact) {
25   LLDB_INSTRUMENT_VA(this, name, is_regex);
26 }
27 
28 SBTypeNameSpecifier::SBTypeNameSpecifier(const char *name,
29                                          FormatterMatchType match_type)
30     : m_opaque_sp(new TypeNameSpecifierImpl(name, match_type)) {
31   LLDB_INSTRUMENT_VA(this, name, match_type);
32 
33   if (name == nullptr || (*name) == 0)
34     m_opaque_sp.reset();
35 }
36 
37 SBTypeNameSpecifier::SBTypeNameSpecifier(SBType type) {
38   LLDB_INSTRUMENT_VA(this, type);
39 
40   if (type.IsValid())
41     m_opaque_sp = TypeNameSpecifierImplSP(
42         new TypeNameSpecifierImpl(type.m_opaque_sp->GetCompilerType(true)));
43 }
44 
45 SBTypeNameSpecifier::SBTypeNameSpecifier(const lldb::SBTypeNameSpecifier &rhs)
46     : m_opaque_sp(rhs.m_opaque_sp) {
47   LLDB_INSTRUMENT_VA(this, rhs);
48 }
49 
50 SBTypeNameSpecifier::~SBTypeNameSpecifier() = default;
51 
52 bool SBTypeNameSpecifier::IsValid() const {
53   LLDB_INSTRUMENT_VA(this);
54   return this->operator bool();
55 }
56 SBTypeNameSpecifier::operator bool() const {
57   LLDB_INSTRUMENT_VA(this);
58 
59   return m_opaque_sp.get() != nullptr;
60 }
61 
62 const char *SBTypeNameSpecifier::GetName() {
63   LLDB_INSTRUMENT_VA(this);
64 
65   if (!IsValid())
66     return nullptr;
67 
68   return ConstString(m_opaque_sp->GetName()).GetCString();
69 }
70 
71 SBType SBTypeNameSpecifier::GetType() {
72   LLDB_INSTRUMENT_VA(this);
73 
74   if (!IsValid())
75     return SBType();
76   lldb_private::CompilerType c_type = m_opaque_sp->GetCompilerType();
77   if (c_type.IsValid())
78     return SBType(c_type);
79   return SBType();
80 }
81 
82 FormatterMatchType SBTypeNameSpecifier::GetMatchType() {
83   LLDB_INSTRUMENT_VA(this);
84   if (!IsValid())
85     return eFormatterMatchExact;
86   return m_opaque_sp->GetMatchType();
87 }
88 
89 bool SBTypeNameSpecifier::IsRegex() {
90   LLDB_INSTRUMENT_VA(this);
91 
92   if (!IsValid())
93     return false;
94 
95   return m_opaque_sp->GetMatchType() == eFormatterMatchRegex;
96 }
97 
98 bool SBTypeNameSpecifier::GetDescription(
99     lldb::SBStream &description, lldb::DescriptionLevel description_level) {
100   LLDB_INSTRUMENT_VA(this, description, description_level);
101 
102   lldb::FormatterMatchType match_type = GetMatchType();
103   const char *match_type_str =
104       (match_type == eFormatterMatchExact   ? "plain"
105        : match_type == eFormatterMatchRegex ? "regex"
106                                             : "callback");
107   if (!IsValid())
108     return false;
109   description.Printf("SBTypeNameSpecifier(%s,%s)", GetName(), match_type_str);
110   return true;
111 }
112 
113 lldb::SBTypeNameSpecifier &SBTypeNameSpecifier::
114 operator=(const lldb::SBTypeNameSpecifier &rhs) {
115   LLDB_INSTRUMENT_VA(this, rhs);
116 
117   if (this != &rhs) {
118     m_opaque_sp = rhs.m_opaque_sp;
119   }
120   return *this;
121 }
122 
123 bool SBTypeNameSpecifier::operator==(lldb::SBTypeNameSpecifier &rhs) {
124   LLDB_INSTRUMENT_VA(this, rhs);
125 
126   if (!IsValid())
127     return !rhs.IsValid();
128   return m_opaque_sp == rhs.m_opaque_sp;
129 }
130 
131 bool SBTypeNameSpecifier::IsEqualTo(lldb::SBTypeNameSpecifier &rhs) {
132   LLDB_INSTRUMENT_VA(this, rhs);
133 
134   if (!IsValid())
135     return !rhs.IsValid();
136 
137   if (GetMatchType() != rhs.GetMatchType())
138     return false;
139   if (GetName() == nullptr || rhs.GetName() == nullptr)
140     return false;
141 
142   return (strcmp(GetName(), rhs.GetName()) == 0);
143 }
144 
145 bool SBTypeNameSpecifier::operator!=(lldb::SBTypeNameSpecifier &rhs) {
146   LLDB_INSTRUMENT_VA(this, rhs);
147 
148   if (!IsValid())
149     return !rhs.IsValid();
150   return m_opaque_sp != rhs.m_opaque_sp;
151 }
152 
153 lldb::TypeNameSpecifierImplSP SBTypeNameSpecifier::GetSP() {
154   return m_opaque_sp;
155 }
156 
157 void SBTypeNameSpecifier::SetSP(
158     const lldb::TypeNameSpecifierImplSP &type_namespec_sp) {
159   m_opaque_sp = type_namespec_sp;
160 }
161 
162 SBTypeNameSpecifier::SBTypeNameSpecifier(
163     const lldb::TypeNameSpecifierImplSP &type_namespec_sp)
164     : m_opaque_sp(type_namespec_sp) {}
165