]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/llvm/tools/lldb/source/API/SBValueList.cpp
Merge bmake-20180512
[FreeBSD/FreeBSD.git] / contrib / llvm / tools / lldb / source / API / SBValueList.cpp
1 //===-- SBValueList.cpp -----------------------------------------*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9
10 #include "lldb/API/SBValueList.h"
11 #include "lldb/API/SBStream.h"
12 #include "lldb/API/SBValue.h"
13 #include "lldb/Core/ValueObjectList.h"
14 #include "lldb/Utility/Log.h"
15
16 #include <vector>
17
18 using namespace lldb;
19 using namespace lldb_private;
20
21 class ValueListImpl {
22 public:
23   ValueListImpl() : m_values() {}
24
25   ValueListImpl(const ValueListImpl &rhs) : m_values(rhs.m_values) {}
26
27   ValueListImpl &operator=(const ValueListImpl &rhs) {
28     if (this == &rhs)
29       return *this;
30     m_values = rhs.m_values;
31     return *this;
32   }
33
34   uint32_t GetSize() { return m_values.size(); }
35
36   void Append(const lldb::SBValue &sb_value) { m_values.push_back(sb_value); }
37
38   void Append(const ValueListImpl &list) {
39     for (auto val : list.m_values)
40       Append(val);
41   }
42
43   lldb::SBValue GetValueAtIndex(uint32_t index) {
44     if (index >= GetSize())
45       return lldb::SBValue();
46     return m_values[index];
47   }
48
49   lldb::SBValue FindValueByUID(lldb::user_id_t uid) {
50     for (auto val : m_values) {
51       if (val.IsValid() && val.GetID() == uid)
52         return val;
53     }
54     return lldb::SBValue();
55   }
56
57   lldb::SBValue GetFirstValueByName(const char *name) const {
58     if (name) {
59       for (auto val : m_values) {
60         if (val.IsValid() && val.GetName() && strcmp(name, val.GetName()) == 0)
61           return val;
62       }
63     }
64     return lldb::SBValue();
65   }
66
67 private:
68   std::vector<lldb::SBValue> m_values;
69 };
70
71 SBValueList::SBValueList() : m_opaque_ap() {}
72
73 SBValueList::SBValueList(const SBValueList &rhs) : m_opaque_ap() {
74   Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
75
76   if (rhs.IsValid())
77     m_opaque_ap.reset(new ValueListImpl(*rhs));
78
79   if (log) {
80     log->Printf(
81         "SBValueList::SBValueList (rhs.ap=%p) => this.ap = %p",
82         static_cast<void *>(rhs.IsValid() ? rhs.m_opaque_ap.get() : NULL),
83         static_cast<void *>(m_opaque_ap.get()));
84   }
85 }
86
87 SBValueList::SBValueList(const ValueListImpl *lldb_object_ptr) : m_opaque_ap() {
88   Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
89
90   if (lldb_object_ptr)
91     m_opaque_ap.reset(new ValueListImpl(*lldb_object_ptr));
92
93   if (log) {
94     log->Printf("SBValueList::SBValueList (lldb_object_ptr=%p) => this.ap = %p",
95                 static_cast<const void *>(lldb_object_ptr),
96                 static_cast<void *>(m_opaque_ap.get()));
97   }
98 }
99
100 SBValueList::~SBValueList() {}
101
102 bool SBValueList::IsValid() const { return (m_opaque_ap.get() != NULL); }
103
104 void SBValueList::Clear() { m_opaque_ap.reset(); }
105
106 const SBValueList &SBValueList::operator=(const SBValueList &rhs) {
107   if (this != &rhs) {
108     if (rhs.IsValid())
109       m_opaque_ap.reset(new ValueListImpl(*rhs));
110     else
111       m_opaque_ap.reset();
112   }
113   return *this;
114 }
115
116 ValueListImpl *SBValueList::operator->() { return m_opaque_ap.get(); }
117
118 ValueListImpl &SBValueList::operator*() { return *m_opaque_ap; }
119
120 const ValueListImpl *SBValueList::operator->() const {
121   return m_opaque_ap.get();
122 }
123
124 const ValueListImpl &SBValueList::operator*() const { return *m_opaque_ap; }
125
126 void SBValueList::Append(const SBValue &val_obj) {
127   CreateIfNeeded();
128   m_opaque_ap->Append(val_obj);
129 }
130
131 void SBValueList::Append(lldb::ValueObjectSP &val_obj_sp) {
132   if (val_obj_sp) {
133     CreateIfNeeded();
134     m_opaque_ap->Append(SBValue(val_obj_sp));
135   }
136 }
137
138 void SBValueList::Append(const lldb::SBValueList &value_list) {
139   if (value_list.IsValid()) {
140     CreateIfNeeded();
141     m_opaque_ap->Append(*value_list);
142   }
143 }
144
145 SBValue SBValueList::GetValueAtIndex(uint32_t idx) const {
146   Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
147
148   // if (log)
149   //    log->Printf ("SBValueList::GetValueAtIndex (uint32_t idx) idx = %d",
150   //    idx);
151
152   SBValue sb_value;
153   if (m_opaque_ap.get())
154     sb_value = m_opaque_ap->GetValueAtIndex(idx);
155
156   if (log) {
157     SBStream sstr;
158     sb_value.GetDescription(sstr);
159     log->Printf("SBValueList::GetValueAtIndex (this.ap=%p, idx=%d) => SBValue "
160                 "(this.sp = %p, '%s')",
161                 static_cast<void *>(m_opaque_ap.get()), idx,
162                 static_cast<void *>(sb_value.GetSP().get()), sstr.GetData());
163   }
164
165   return sb_value;
166 }
167
168 uint32_t SBValueList::GetSize() const {
169   Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
170
171   // if (log)
172   //    log->Printf ("SBValueList::GetSize ()");
173
174   uint32_t size = 0;
175   if (m_opaque_ap.get())
176     size = m_opaque_ap->GetSize();
177
178   if (log)
179     log->Printf("SBValueList::GetSize (this.ap=%p) => %d",
180                 static_cast<void *>(m_opaque_ap.get()), size);
181
182   return size;
183 }
184
185 void SBValueList::CreateIfNeeded() {
186   if (m_opaque_ap.get() == NULL)
187     m_opaque_ap.reset(new ValueListImpl());
188 }
189
190 SBValue SBValueList::FindValueObjectByUID(lldb::user_id_t uid) {
191   SBValue sb_value;
192   if (m_opaque_ap.get())
193     sb_value = m_opaque_ap->FindValueByUID(uid);
194   return sb_value;
195 }
196
197 SBValue SBValueList::GetFirstValueByName(const char *name) const {
198   SBValue sb_value;
199   if (m_opaque_ap.get())
200     sb_value = m_opaque_ap->GetFirstValueByName(name);
201   return sb_value;
202 }
203
204 void *SBValueList::opaque_ptr() { return m_opaque_ap.get(); }
205
206 ValueListImpl &SBValueList::ref() {
207   CreateIfNeeded();
208   return *m_opaque_ap.get();
209 }