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