1 //===-- ValueObjectConstResult.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/Core/ValueObjectConstResult.h"
11 
12 #include "lldb/Core/ValueObjectChild.h"
13 #include "lldb/Core/ValueObjectConstResultChild.h"
14 #include "lldb/Core/DataExtractor.h"
15 #include "lldb/Core/Module.h"
16 #include "lldb/Core/ValueObjectDynamicValue.h"
17 #include "lldb/Core/ValueObjectList.h"
18 
19 #include "lldb/Symbol/ClangASTType.h"
20 #include "lldb/Symbol/ObjectFile.h"
21 #include "lldb/Symbol/SymbolContext.h"
22 #include "lldb/Symbol/Type.h"
23 #include "lldb/Symbol/Variable.h"
24 
25 #include "lldb/Target/ExecutionContext.h"
26 #include "lldb/Target/Process.h"
27 #include "lldb/Target/Target.h"
28 
29 using namespace lldb;
30 using namespace lldb_private;
31 
32 ValueObjectSP
33 ValueObjectConstResult::Create (ExecutionContextScope *exe_scope,
34                                 ByteOrder byte_order,
35                                 uint32_t addr_byte_size,
36                                 lldb::addr_t address)
37 {
38     return (new ValueObjectConstResult (exe_scope,
39                                         byte_order,
40                                         addr_byte_size,
41                                         address))->GetSP();
42 }
43 
44 ValueObjectConstResult::ValueObjectConstResult (ExecutionContextScope *exe_scope,
45                                                 ByteOrder byte_order,
46                                                 uint32_t addr_byte_size,
47                                                 lldb::addr_t address) :
48     ValueObject (exe_scope),
49     m_type_name (),
50     m_byte_size (0),
51     m_impl(this, address)
52 {
53     SetIsConstant ();
54     SetValueIsValid(true);
55     m_data.SetByteOrder(byte_order);
56     m_data.SetAddressByteSize(addr_byte_size);
57     SetAddressTypeOfChildren(eAddressTypeLoad);
58 }
59 
60 ValueObjectSP
61 ValueObjectConstResult::Create
62 (
63     ExecutionContextScope *exe_scope,
64     const ClangASTType &clang_type,
65     const ConstString &name,
66     const DataExtractor &data,
67     lldb::addr_t address
68 )
69 {
70     return (new ValueObjectConstResult (exe_scope,
71                                         clang_type,
72                                         name,
73                                         data,
74                                         address))->GetSP();
75 }
76 
77 ValueObjectConstResult::ValueObjectConstResult (ExecutionContextScope *exe_scope,
78                                                 const ClangASTType &clang_type,
79                                                 const ConstString &name,
80                                                 const DataExtractor &data,
81                                                 lldb::addr_t address) :
82     ValueObject (exe_scope),
83     m_type_name (),
84     m_byte_size (0),
85     m_impl(this, address)
86 {
87     m_data = data;
88 
89     if (!m_data.GetSharedDataBuffer())
90     {
91         DataBufferSP shared_data_buffer(new DataBufferHeap(data.GetDataStart(), data.GetByteSize()));
92         m_data.SetData(shared_data_buffer);
93     }
94 
95     m_value.GetScalar() = (uintptr_t)m_data.GetDataStart();
96     m_value.SetValueType(Value::eValueTypeHostAddress);
97     m_value.SetClangType(clang_type);
98     m_name = name;
99     SetIsConstant ();
100     SetValueIsValid(true);
101     SetAddressTypeOfChildren(eAddressTypeLoad);
102 }
103 
104 ValueObjectSP
105 ValueObjectConstResult::Create (ExecutionContextScope *exe_scope,
106                                 const ClangASTType &clang_type,
107                                 const ConstString &name,
108                                 const lldb::DataBufferSP &data_sp,
109                                 lldb::ByteOrder data_byte_order,
110                                 uint32_t data_addr_size,
111                                 lldb::addr_t address)
112 {
113     return (new ValueObjectConstResult (exe_scope,
114                                         clang_type,
115                                         name,
116                                         data_sp,
117                                         data_byte_order,
118                                         data_addr_size,
119                                         address))->GetSP();
120 }
121 
122 ValueObjectSP
123 ValueObjectConstResult::Create (ExecutionContextScope *exe_scope,
124                                 Value &value,
125                                 const ConstString &name)
126 {
127     return (new ValueObjectConstResult (exe_scope, value, name))->GetSP();
128 }
129 
130 ValueObjectConstResult::ValueObjectConstResult (ExecutionContextScope *exe_scope,
131                                                 const ClangASTType &clang_type,
132                                                 const ConstString &name,
133                                                 const lldb::DataBufferSP &data_sp,
134                                                 lldb::ByteOrder data_byte_order,
135                                                 uint32_t data_addr_size,
136                                                 lldb::addr_t address) :
137     ValueObject (exe_scope),
138     m_type_name (),
139     m_byte_size (0),
140     m_impl(this, address)
141 {
142     m_data.SetByteOrder(data_byte_order);
143     m_data.SetAddressByteSize(data_addr_size);
144     m_data.SetData(data_sp);
145     m_value.GetScalar() = (uintptr_t)data_sp->GetBytes();
146     m_value.SetValueType(Value::eValueTypeHostAddress);
147     //m_value.SetContext(Value::eContextTypeClangType, clang_type);
148     m_value.SetClangType (clang_type);
149     m_name = name;
150     SetIsConstant ();
151     SetValueIsValid(true);
152     SetAddressTypeOfChildren(eAddressTypeLoad);
153 }
154 
155 ValueObjectSP
156 ValueObjectConstResult::Create (ExecutionContextScope *exe_scope,
157                                 const ClangASTType &clang_type,
158                                 const ConstString &name,
159                                 lldb::addr_t address,
160                                 AddressType address_type,
161                                 uint32_t addr_byte_size)
162 {
163     return (new ValueObjectConstResult (exe_scope,
164                                         clang_type,
165                                         name,
166                                         address,
167                                         address_type,
168                                         addr_byte_size))->GetSP();
169 }
170 
171 ValueObjectConstResult::ValueObjectConstResult (ExecutionContextScope *exe_scope,
172                                                 const ClangASTType &clang_type,
173                                                 const ConstString &name,
174                                                 lldb::addr_t address,
175                                                 AddressType address_type,
176                                                 uint32_t addr_byte_size) :
177     ValueObject (exe_scope),
178     m_type_name (),
179     m_byte_size (0),
180     m_impl(this, address)
181 {
182     m_value.GetScalar() = address;
183     m_data.SetAddressByteSize(addr_byte_size);
184     m_value.GetScalar().GetData (m_data, addr_byte_size);
185     //m_value.SetValueType(Value::eValueTypeHostAddress);
186     switch (address_type)
187     {
188     case eAddressTypeInvalid:   m_value.SetValueType(Value::eValueTypeScalar);      break;
189     case eAddressTypeFile:      m_value.SetValueType(Value::eValueTypeFileAddress); break;
190     case eAddressTypeLoad:      m_value.SetValueType(Value::eValueTypeLoadAddress); break;
191     case eAddressTypeHost:      m_value.SetValueType(Value::eValueTypeHostAddress); break;
192     }
193 //    m_value.SetContext(Value::eContextTypeClangType, clang_type);
194     m_value.SetClangType (clang_type);
195     m_name = name;
196     SetIsConstant ();
197     SetValueIsValid(true);
198     SetAddressTypeOfChildren(eAddressTypeLoad);
199 }
200 
201 ValueObjectSP
202 ValueObjectConstResult::Create
203 (
204     ExecutionContextScope *exe_scope,
205     const Error& error
206 )
207 {
208     return (new ValueObjectConstResult (exe_scope,
209                                         error))->GetSP();
210 }
211 
212 ValueObjectConstResult::ValueObjectConstResult (ExecutionContextScope *exe_scope,
213                                                 const Error& error) :
214     ValueObject (exe_scope),
215     m_type_name (),
216     m_byte_size (0),
217     m_impl(this)
218 {
219     m_error = error;
220     SetIsConstant ();
221 }
222 
223 ValueObjectConstResult::ValueObjectConstResult (ExecutionContextScope *exe_scope,
224                                                 const Value &value,
225                                                 const ConstString &name) :
226     ValueObject (exe_scope),
227     m_type_name (),
228     m_byte_size (0),
229     m_impl(this)
230 {
231     m_value = value;
232     m_value.GetData(m_data);
233     m_name = name;
234 }
235 
236 ValueObjectConstResult::~ValueObjectConstResult()
237 {
238 }
239 
240 ClangASTType
241 ValueObjectConstResult::GetClangTypeImpl()
242 {
243     return m_value.GetClangType();
244 }
245 
246 lldb::ValueType
247 ValueObjectConstResult::GetValueType() const
248 {
249     return eValueTypeConstResult;
250 }
251 
252 uint64_t
253 ValueObjectConstResult::GetByteSize()
254 {
255     if (m_byte_size == 0)
256         m_byte_size = GetClangType().GetByteSize();
257     return m_byte_size;
258 }
259 
260 void
261 ValueObjectConstResult::SetByteSize (size_t size)
262 {
263     m_byte_size = size;
264 }
265 
266 size_t
267 ValueObjectConstResult::CalculateNumChildren()
268 {
269     return GetClangType().GetNumChildren (true);
270 }
271 
272 ConstString
273 ValueObjectConstResult::GetTypeName()
274 {
275     if (m_type_name.IsEmpty())
276         m_type_name = GetClangType().GetConstTypeName ();
277     return m_type_name;
278 }
279 
280 ConstString
281 ValueObjectConstResult::GetDisplayTypeName()
282 {
283     return GetClangType().GetDisplayTypeName();
284 }
285 
286 bool
287 ValueObjectConstResult::UpdateValue ()
288 {
289     // Const value is always valid
290     SetValueIsValid (true);
291     return true;
292 }
293 
294 
295 bool
296 ValueObjectConstResult::IsInScope ()
297 {
298     // A const result value is always in scope since it serializes all
299     // information needed to contain the constant value.
300     return true;
301 }
302 
303 lldb::ValueObjectSP
304 ValueObjectConstResult::Dereference (Error &error)
305 {
306     return m_impl.Dereference(error);
307 }
308 
309 lldb::ValueObjectSP
310 ValueObjectConstResult::GetSyntheticChildAtOffset(uint32_t offset, const ClangASTType& type, bool can_create)
311 {
312     return m_impl.GetSyntheticChildAtOffset(offset, type, can_create);
313 }
314 
315 lldb::ValueObjectSP
316 ValueObjectConstResult::AddressOf (Error &error)
317 {
318     return m_impl.AddressOf(error);
319 }
320 
321 lldb::addr_t
322 ValueObjectConstResult::GetAddressOf (bool scalar_is_load_address,
323                                       AddressType *address_type)
324 {
325     return m_impl.GetAddressOf(scalar_is_load_address, address_type);
326 }
327 
328 ValueObject *
329 ValueObjectConstResult::CreateChildAtIndex (size_t idx, bool synthetic_array_member, int32_t synthetic_index)
330 {
331     return m_impl.CreateChildAtIndex(idx, synthetic_array_member, synthetic_index);
332 }
333 
334 size_t
335 ValueObjectConstResult::GetPointeeData (DataExtractor& data,
336                                         uint32_t item_idx,
337                                         uint32_t item_count)
338 {
339     return m_impl.GetPointeeData(data, item_idx, item_count);
340 }
341 
342 lldb::ValueObjectSP
343 ValueObjectConstResult::GetDynamicValue (lldb::DynamicValueType use_dynamic)
344 {
345     // Always recalculate dynamic values for const results as the memory that
346     // they might point to might have changed at any time.
347     if (use_dynamic != eNoDynamicValues)
348     {
349         if (!IsDynamic())
350         {
351             ExecutionContext exe_ctx (GetExecutionContextRef());
352             Process *process = exe_ctx.GetProcessPtr();
353             if (process && process->IsPossibleDynamicValue(*this))
354                 m_dynamic_value = new ValueObjectDynamicValue (*this, use_dynamic);
355         }
356         if (m_dynamic_value)
357             return m_dynamic_value->GetSP();
358     }
359     return ValueObjectSP();
360 }
361 
362