blob: 41483c7c7ccfbff828bec733e3e7017b86d605c5 [file] [log] [blame]
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001//===-- Value.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/Value.h"
11
12// C Includes
13// C++ Includes
14// Other libraries and framework includes
15// Project includes
16#include "lldb/Core/DataExtractor.h"
17#include "lldb/Core/DataBufferHeap.h"
18#include "lldb/Core/Module.h"
19#include "lldb/Core/Stream.h"
Greg Claytone1a916a2010-07-21 22:12:05 +000020#include "lldb/Symbol/ClangASTType.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000021#include "lldb/Symbol/ClangASTContext.h"
22#include "lldb/Symbol/ObjectFile.h"
23#include "lldb/Symbol/SymbolContext.h"
24#include "lldb/Symbol/Type.h"
25#include "lldb/Symbol/Variable.h"
26#include "lldb/Target/ExecutionContext.h"
27#include "lldb/Target/Process.h"
28
29using namespace lldb;
30using namespace lldb_private;
31
32Value::Value() :
33 m_value(),
34 m_value_type(eValueTypeScalar),
35 m_context(NULL),
36 m_context_type(eContextTypeInvalid)
37{
38}
39
40Value::Value(const Scalar& scalar) :
41 m_value(scalar),
42 m_value_type(eValueTypeScalar),
43 m_context(NULL),
44 m_context_type(eContextTypeInvalid)
45{
46}
47
48Value::Value(int v) :
49 m_value(v),
50 m_value_type(eValueTypeScalar),
51 m_context(NULL),
52 m_context_type(eContextTypeInvalid)
53{
54}
55
56Value::Value(unsigned int v) :
57 m_value(v),
58 m_value_type(eValueTypeScalar),
59 m_context(NULL),
60 m_context_type(eContextTypeInvalid)
61{
62}
63
64Value::Value(long v) :
65 m_value(v),
66 m_value_type(eValueTypeScalar),
67 m_context(NULL),
68 m_context_type(eContextTypeInvalid)
69{
70}
71
72Value::Value(unsigned long v) :
73 m_value(v),
74 m_value_type(eValueTypeScalar),
75 m_context(NULL),
76 m_context_type(eContextTypeInvalid)
77{
78}
79
80Value::Value(long long v) :
81 m_value(v),
82 m_value_type(eValueTypeScalar),
83 m_context(NULL),
84 m_context_type(eContextTypeInvalid)
85{
86}
87
88Value::Value(unsigned long long v) :
89 m_value(v),
90 m_value_type(eValueTypeScalar),
91 m_context(NULL),
92 m_context_type(eContextTypeInvalid)
93{
94}
95
96Value::Value(float v) :
97 m_value(v),
98 m_value_type(eValueTypeScalar),
99 m_context(NULL),
100 m_context_type(eContextTypeInvalid)
101{
102}
103
104Value::Value(double v) :
105 m_value(v),
106 m_value_type(eValueTypeScalar),
107 m_context(NULL),
108 m_context_type(eContextTypeInvalid)
109{
110}
111
112Value::Value(long double v) :
113 m_value(v),
114 m_value_type(eValueTypeScalar),
115 m_context(NULL),
116 m_context_type(eContextTypeInvalid)
117{
118}
119
120Value::Value(const uint8_t *bytes, int len) :
121 m_value(),
122 m_value_type(eValueTypeHostAddress),
123 m_context(NULL),
124 m_context_type(eContextTypeInvalid)
125{
126 m_data_buffer.CopyData(bytes, len);
127 m_value = (uintptr_t)m_data_buffer.GetBytes();
128}
129
130Value::Value(const Value &v) :
131 m_value(v.m_value),
132 m_value_type(v.m_value_type),
133 m_context(v.m_context),
134 m_context_type(v.m_context_type)
135{
136 if ((uintptr_t)v.m_value.ULongLong(LLDB_INVALID_ADDRESS) == (uintptr_t)v.m_data_buffer.GetBytes())
137 {
138 m_data_buffer.CopyData(v.m_data_buffer.GetBytes(),
139 v.m_data_buffer.GetByteSize());
140
141 m_value = (uintptr_t)m_data_buffer.GetBytes();
142 }
143}
144
Greg Clayton1a65ae12011-01-25 23:55:37 +0000145Value &
146Value::operator=(const Value &rhs)
147{
148 if (this != &rhs)
149 {
150 m_value = rhs.m_value;
151 m_value_type = rhs.m_value_type;
152 m_context = rhs.m_context;
153 m_context_type = rhs.m_context_type;
154 if ((uintptr_t)rhs.m_value.ULongLong(LLDB_INVALID_ADDRESS) == (uintptr_t)rhs.m_data_buffer.GetBytes())
155 {
156 m_data_buffer.CopyData(rhs.m_data_buffer.GetBytes(),
157 rhs.m_data_buffer.GetByteSize());
158
159 m_value = (uintptr_t)m_data_buffer.GetBytes();
160 }
161 }
162 return *this;
163}
164
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000165Value *
166Value::CreateProxy()
167{
168 if (m_context_type == eContextTypeValue)
169 return ((Value*)m_context)->CreateProxy ();
170
171 Value *ret = new Value;
172 ret->SetContext(eContextTypeValue, this);
173 return ret;
174}
175
176Value *
177Value::GetProxyTarget()
178{
179 if (m_context_type == eContextTypeValue)
180 return (Value*)m_context;
181 else
182 return NULL;
183}
184
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000185void
186Value::Dump (Stream* strm)
187{
188 if (m_context_type == eContextTypeValue)
189 {
190 ((Value*)m_context)->Dump (strm);
191 return;
192 }
193
194 m_value.GetValue (strm, true);
195 strm->Printf(", value_type = %s, context = %p, context_type = %s",
196 Value::GetValueTypeAsCString(m_value_type),
197 m_context,
198 Value::GetContextTypeAsCString(m_context_type));
199}
200
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000201Value::ValueType
202Value::GetValueType() const
203{
204 if (m_context_type == eContextTypeValue)
205 return ((Value*)m_context)->GetValueType ();
206
207 return m_value_type;
208}
209
210lldb::AddressType
211Value::GetValueAddressType () const
212{
213 if (m_context_type == eContextTypeValue)
214 return ((Value*)m_context)->GetValueAddressType ();
215
216 switch (m_value_type)
217 {
218 default:
219 case eValueTypeScalar:
220 break;
221 case eValueTypeLoadAddress: return eAddressTypeLoad;
222 case eValueTypeFileAddress: return eAddressTypeFile;
223 case eValueTypeHostAddress: return eAddressTypeHost;
224 }
225 return eAddressTypeInvalid;
226}
227
228
229Value::ContextType
230Value::GetContextType() const
231{
232 if (m_context_type == eContextTypeValue)
233 return ((Value*)m_context)->GetContextType ();
234
235 return m_context_type;
236}
237
238void
239Value::SetValueType (Value::ValueType value_type)
240{
241 if (m_context_type == eContextTypeValue)
242 {
243 ((Value*)m_context)->SetValueType(value_type);
244 return;
245 }
246
247 m_value_type = value_type;
248}
249
250void
251Value::ClearContext ()
252{
253 if (m_context_type == eContextTypeValue)
254 {
255 ((Value*)m_context)->ClearContext();
256 return;
257 }
258
259 m_context = NULL;
260 m_context_type = eContextTypeInvalid;
261}
262
263void
264Value::SetContext (Value::ContextType context_type, void *p)
265{
266 if (m_context_type == eContextTypeValue)
267 {
268 ((Value*)m_context)->SetContext(context_type, p);
269 return;
270 }
271
272 m_context_type = context_type;
273 m_context = p;
274}
275
276RegisterInfo *
277Value::GetRegisterInfo()
278{
279 if (m_context_type == eContextTypeValue)
280 return ((Value*)m_context)->GetRegisterInfo();
281
Greg Clayton526e5af2010-11-13 03:52:47 +0000282 if (m_context_type == eContextTypeRegisterInfo)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000283 return static_cast<RegisterInfo *> (m_context);
284 return NULL;
285}
286
287Type *
288Value::GetType()
289{
290 if (m_context_type == eContextTypeValue)
291 return ((Value*)m_context)->GetType();
292
Greg Clayton526e5af2010-11-13 03:52:47 +0000293 if (m_context_type == eContextTypeLLDBType)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000294 return static_cast<Type *> (m_context);
295 return NULL;
296}
297
298Scalar &
299Value::GetScalar()
300{
301 if (m_context_type == eContextTypeValue)
302 return ((Value*)m_context)->GetScalar();
303
304 return m_value;
305}
306
307void
308Value::ResizeData(int len)
309{
310 if (m_context_type == eContextTypeValue)
311 {
312 ((Value*)m_context)->ResizeData(len);
313 return;
314 }
315
316 m_value_type = eValueTypeHostAddress;
317 m_data_buffer.SetByteSize(len);
318 m_value = (uintptr_t)m_data_buffer.GetBytes();
319}
320
321bool
322Value::ValueOf(ExecutionContext *exe_ctx, clang::ASTContext *ast_context)
323{
324 if (m_context_type == eContextTypeValue)
325 return ((Value*)m_context)->ValueOf(exe_ctx, ast_context);
326
327 switch (m_context_type)
328 {
329 default:
330 case eContextTypeInvalid:
Greg Clayton526e5af2010-11-13 03:52:47 +0000331 case eContextTypeClangType: // clang::Type *
332 case eContextTypeRegisterInfo: // RegisterInfo *
333 case eContextTypeLLDBType: // Type *
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000334 break;
335
Greg Clayton526e5af2010-11-13 03:52:47 +0000336 case eContextTypeVariable: // Variable *
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000337 ResolveValue(exe_ctx, ast_context);
338 return true;
339 }
340 return false;
341}
342
343size_t
344Value::GetValueByteSize (clang::ASTContext *ast_context, Error *error_ptr)
345{
346 if (m_context_type == eContextTypeValue)
347 return ((Value*)m_context)->GetValueByteSize(ast_context, error_ptr);
348
349 size_t byte_size = 0;
350
351 switch (m_context_type)
352 {
353 default:
354 case eContextTypeInvalid:
355 // If we have no context, there is no way to know how much memory to read
356 if (error_ptr)
357 error_ptr->SetErrorString ("Invalid context type, there is no way to know how much memory to read.");
358 break;
359
Greg Clayton526e5af2010-11-13 03:52:47 +0000360 case eContextTypeClangType:
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000361 if (ast_context == NULL)
362 {
363 if (error_ptr)
364 error_ptr->SetErrorString ("Can't determine size of opaque clang type with NULL ASTContext *.");
365 }
366 else
367 {
Greg Claytonb0b9fe62010-08-03 00:35:52 +0000368 uint64_t bit_width = ClangASTType::GetClangTypeBitWidth (ast_context, m_context);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000369 byte_size = (bit_width + 7 ) / 8;
370 }
371 break;
372
Greg Clayton526e5af2010-11-13 03:52:47 +0000373 case eContextTypeRegisterInfo: // RegisterInfo *
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000374 if (GetRegisterInfo())
375 byte_size = GetRegisterInfo()->byte_size;
376 else if (error_ptr)
377 error_ptr->SetErrorString ("Can't determine byte size with NULL RegisterInfo *.");
378
379 break;
380
Greg Clayton526e5af2010-11-13 03:52:47 +0000381 case eContextTypeLLDBType: // Type *
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000382 if (GetType())
383 byte_size = GetType()->GetByteSize();
384 else if (error_ptr)
385 error_ptr->SetErrorString ("Can't determine byte size with NULL Type *.");
386 break;
387
Greg Clayton526e5af2010-11-13 03:52:47 +0000388 case eContextTypeVariable: // Variable *
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000389 if (GetVariable())
390 byte_size = GetVariable()->GetType()->GetByteSize();
391 else if (error_ptr)
392 error_ptr->SetErrorString ("Can't determine byte size with NULL Variable *.");
393 break;
394 }
395
396 if (error_ptr)
397 {
398 if (byte_size == 0)
399 {
400 if (error_ptr->Success())
401 error_ptr->SetErrorString("Unable to determine byte size.");
402 }
403 else
404 {
405 error_ptr->Clear();
406 }
407 }
408 return byte_size;
409}
410
411void *
Greg Clayton1be10fc2010-09-29 01:12:09 +0000412Value::GetClangType ()
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000413{
414 if (m_context_type == eContextTypeValue)
Greg Clayton1be10fc2010-09-29 01:12:09 +0000415 return ((Value*)m_context)->GetClangType();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000416
417 switch (m_context_type)
418 {
419 default:
420 case eContextTypeInvalid:
421 break;
422
Greg Clayton526e5af2010-11-13 03:52:47 +0000423 case eContextTypeClangType:
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000424 return m_context;
425
Greg Clayton526e5af2010-11-13 03:52:47 +0000426 case eContextTypeRegisterInfo:
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000427 break; // TODO: Eventually convert into a clang type?
428
Greg Clayton526e5af2010-11-13 03:52:47 +0000429 case eContextTypeLLDBType:
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000430 if (GetType())
Greg Clayton1be10fc2010-09-29 01:12:09 +0000431 return GetType()->GetClangType();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000432 break;
433
Greg Clayton526e5af2010-11-13 03:52:47 +0000434 case eContextTypeVariable:
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000435 if (GetVariable())
Greg Clayton1be10fc2010-09-29 01:12:09 +0000436 return GetVariable()->GetType()->GetClangType();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000437 break;
438 }
439
440 return NULL;
441}
442
443lldb::Format
444Value::GetValueDefaultFormat ()
445{
446 if (m_context_type == eContextTypeValue)
447 return ((Value*)m_context)->GetValueDefaultFormat();
448
449 switch (m_context_type)
450 {
451 default:
452 case eContextTypeInvalid:
453 break;
454
Greg Clayton526e5af2010-11-13 03:52:47 +0000455 case eContextTypeClangType:
Greg Claytone1a916a2010-07-21 22:12:05 +0000456 return ClangASTType::GetFormat (m_context);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000457
Greg Clayton526e5af2010-11-13 03:52:47 +0000458 case eContextTypeRegisterInfo:
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000459 if (GetRegisterInfo())
460 return GetRegisterInfo()->format;
461 break;
462
Greg Clayton526e5af2010-11-13 03:52:47 +0000463 case eContextTypeLLDBType:
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000464 if (GetType())
465 return GetType()->GetFormat();
466 break;
467
Greg Clayton526e5af2010-11-13 03:52:47 +0000468 case eContextTypeVariable:
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000469 if (GetVariable())
470 return GetVariable()->GetType()->GetFormat();
471 break;
472
473 }
474
475 // Return a good default in case we can't figure anything out
476 return eFormatHex;
477}
478
Greg Clayton8b2fe6d2010-12-14 02:59:59 +0000479bool
480Value::GetData (DataExtractor &data)
481{
482 switch (m_value_type)
483 {
484 default:
485 break;
486
487 case eValueTypeScalar:
488 if (m_value.GetData (data))
489 return true;
490 break;
491
492 case eValueTypeLoadAddress:
493 case eValueTypeFileAddress:
494 case eValueTypeHostAddress:
495 if (m_data_buffer.GetByteSize())
496 {
497 data.SetData(m_data_buffer.GetBytes(), m_data_buffer.GetByteSize(), data.GetByteOrder());
498 return true;
499 }
500 break;
501 }
502
503 return false;
504
505}
506
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000507Error
508Value::GetValueAsData (ExecutionContext *exe_ctx, clang::ASTContext *ast_context, DataExtractor &data, uint32_t data_offset)
509{
510 if (m_context_type == eContextTypeValue)
511 return ((Value*)m_context)->GetValueAsData(exe_ctx, ast_context, data, data_offset);
512
513 data.Clear();
514
515 Error error;
516 lldb::addr_t address = LLDB_INVALID_ADDRESS;
517 lldb::AddressType address_type = eAddressTypeFile;
518 switch (m_value_type)
519 {
520 default:
Greg Clayton7c8a9662010-11-02 01:50:16 +0000521 error.SetErrorStringWithFormat("invalid value type %i", m_value_type);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000522 break;
523
524 case eValueTypeScalar:
525 data.SetByteOrder (eByteOrderHost);
526 data.SetAddressByteSize(sizeof(void *));
527 if (m_value.GetData (data))
528 return error; // Success;
Greg Clayton7c8a9662010-11-02 01:50:16 +0000529 error.SetErrorStringWithFormat("extracting data from value failed");
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000530 break;
531
532 case eValueTypeLoadAddress:
533 if (exe_ctx == NULL)
534 {
Greg Clayton7c8a9662010-11-02 01:50:16 +0000535 error.SetErrorString ("can't read memory (no execution context)");
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000536 }
537 else if (exe_ctx->process == NULL)
538 {
Greg Clayton7c8a9662010-11-02 01:50:16 +0000539 error.SetErrorString ("can't read memory (invalid process)");
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000540 }
541 else
542 {
543 address = m_value.ULongLong(LLDB_INVALID_ADDRESS);
544 address_type = eAddressTypeLoad;
545 data.SetByteOrder(exe_ctx->process->GetByteOrder());
546 data.SetAddressByteSize(exe_ctx->process->GetAddressByteSize());
547 }
548 break;
549
550 case eValueTypeFileAddress:
551 {
552 // The only thing we can currently lock down to a module so that
553 // we can resolve a file address, is a variable.
554 Variable *variable = GetVariable();
555
556 if (GetVariable())
557 {
558 lldb::addr_t file_addr = m_value.ULongLong(LLDB_INVALID_ADDRESS);
559 if (file_addr != LLDB_INVALID_ADDRESS)
560 {
561 SymbolContext var_sc;
562 variable->CalculateSymbolContext(&var_sc);
563 if (var_sc.module_sp)
564 {
565 ObjectFile *objfile = var_sc.module_sp->GetObjectFile();
566 if (objfile)
567 {
568 Address so_addr(file_addr, objfile->GetSectionList());
Greg Claytonf5e56de2010-09-14 23:36:40 +0000569 address = so_addr.GetLoadAddress (exe_ctx->target);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000570 if (address != LLDB_INVALID_ADDRESS)
Greg Claytonad3843c2010-08-18 18:28:52 +0000571 {
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000572 address_type = eAddressTypeLoad;
Greg Claytonad3843c2010-08-18 18:28:52 +0000573 data.SetByteOrder(exe_ctx->process->GetByteOrder());
574 data.SetAddressByteSize(exe_ctx->process->GetAddressByteSize());
575 }
576 else
577 {
578 data.SetByteOrder(objfile->GetByteOrder());
579 data.SetAddressByteSize(objfile->GetAddressByteSize());
580 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000581 }
582 if (address_type == eAddressTypeFile)
583 error.SetErrorStringWithFormat ("%s is not loaded.\n", var_sc.module_sp->GetFileSpec().GetFilename().AsCString());
584 }
585 else
586 {
Greg Clayton7c8a9662010-11-02 01:50:16 +0000587 error.SetErrorStringWithFormat ("unable to resolve the module for file address 0x%llx for variable '%s'", file_addr, variable->GetName().AsCString(""));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000588 }
589 }
590 else
591 {
592 error.SetErrorString ("Invalid file address.");
593 }
594 }
595 else
596 {
597 // Can't convert a file address to anything valid without more
598 // context (which Module it came from)
Greg Clayton7c8a9662010-11-02 01:50:16 +0000599 error.SetErrorString ("can't read memory from file address without more context");
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000600 }
601 }
602 break;
603
604 case eValueTypeHostAddress:
605 address = m_value.ULongLong(LLDB_INVALID_ADDRESS);
606 data.SetByteOrder(eByteOrderHost);
607 data.SetAddressByteSize(sizeof(void *));
608 address_type = eAddressTypeHost;
609 break;
610 }
611
612 // Bail if we encountered any errors
613 if (error.Fail())
614 return error;
615
616 if (address == LLDB_INVALID_ADDRESS)
617 {
Greg Clayton7c8a9662010-11-02 01:50:16 +0000618 error.SetErrorStringWithFormat ("invalid %s address", address_type == eAddressTypeHost ? "host" : "load");
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000619 return error;
620 }
621
622 // If we got here, we need to read the value from memory
623 uint32_t byte_size = GetValueByteSize (ast_context, &error);
624
625 // Bail if we encountered any errors getting the byte size
626 if (error.Fail())
627 return error;
628
629 // Make sure we have enough room within "data", and if we don't make
630 // something large enough that does
631 if (!data.ValidOffsetForDataOfSize (data_offset, byte_size))
632 {
633 DataBufferSP data_sp(new DataBufferHeap (data_offset + byte_size, '\0'));
634 data.SetData(data_sp);
635 }
636
Greg Claytonc982c762010-07-09 20:39:50 +0000637 uint8_t* dst = const_cast<uint8_t*>(data.PeekData (data_offset, byte_size));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000638 if (dst != NULL)
639 {
640 if (address_type == eAddressTypeHost)
641 {
642 // The address is an address in this process, so just copy it
643 memcpy (dst, (uint8_t*)NULL + address, byte_size);
644 }
645 else if (address_type == eAddressTypeLoad)
646 {
647 if (exe_ctx->process->ReadMemory(address, dst, byte_size, error) != byte_size)
648 {
649 if (error.Success())
650 error.SetErrorStringWithFormat("read %u bytes of memory from 0x%llx failed", (uint64_t)address, byte_size);
Greg Clayton7c8a9662010-11-02 01:50:16 +0000651 else
652 error.SetErrorStringWithFormat("read memory from 0x%llx failed", (uint64_t)address);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000653 }
654 }
655 else
656 {
Greg Clayton7c8a9662010-11-02 01:50:16 +0000657 error.SetErrorStringWithFormat ("unsupported lldb::AddressType value (%i)", address_type);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000658 }
659 }
660 else
661 {
Greg Clayton7c8a9662010-11-02 01:50:16 +0000662 error.SetErrorStringWithFormat ("out of memory");
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000663 }
664
665 return error;
666}
667
668Scalar &
669Value::ResolveValue(ExecutionContext *exe_ctx, clang::ASTContext *ast_context)
670{
671 Scalar scalar;
672 if (m_context_type == eContextTypeValue)
673 {
674 // Resolve the proxy
675
Greg Clayton1a65ae12011-01-25 23:55:37 +0000676 Value * rhs = (Value*)m_context;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000677
Greg Clayton1a65ae12011-01-25 23:55:37 +0000678 m_value = rhs->m_value;
679 m_value_type = rhs->m_value_type;
680 m_context = rhs->m_context;
681 m_context_type = rhs->m_context_type;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000682
Greg Clayton1a65ae12011-01-25 23:55:37 +0000683 if ((uintptr_t)rhs->m_value.ULongLong(LLDB_INVALID_ADDRESS) == (uintptr_t)rhs->m_data_buffer.GetBytes())
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000684 {
Greg Clayton1a65ae12011-01-25 23:55:37 +0000685 m_data_buffer.CopyData(rhs->m_data_buffer.GetBytes(),
686 rhs->m_data_buffer.GetByteSize());
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000687
688 m_value = (uintptr_t)m_data_buffer.GetBytes();
689 }
690 }
691
Greg Clayton526e5af2010-11-13 03:52:47 +0000692 if (m_context_type == eContextTypeClangType)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000693 {
Greg Clayton1be10fc2010-09-29 01:12:09 +0000694 void *opaque_clang_qual_type = GetClangType();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000695 switch (m_value_type)
696 {
697 case eValueTypeScalar: // raw scalar value
698 break;
699
700 default:
701 case eValueTypeFileAddress:
702 m_value.Clear();
703 break;
704
705 case eValueTypeLoadAddress: // load address value
706 case eValueTypeHostAddress: // host address value (for memory in the process that is using liblldb)
707 {
708 lldb::AddressType address_type = m_value_type == eValueTypeLoadAddress ? eAddressTypeLoad : eAddressTypeHost;
709 lldb::addr_t addr = m_value.ULongLong(LLDB_INVALID_ADDRESS);
710 DataExtractor data;
Greg Claytone1a916a2010-07-21 22:12:05 +0000711 if (ClangASTType::ReadFromMemory (ast_context, opaque_clang_qual_type, exe_ctx, addr, address_type, data))
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000712 {
Greg Claytone1a916a2010-07-21 22:12:05 +0000713 if (ClangASTType::GetValueAsScalar (ast_context, opaque_clang_qual_type, data, 0, data.GetByteSize(), scalar))
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000714 {
715 m_value = scalar;
716 m_value_type = eValueTypeScalar;
717 }
718 else
719 {
720 if ((uintptr_t)addr != (uintptr_t)m_data_buffer.GetBytes())
721 {
722 m_value.Clear();
723 m_value_type = eValueTypeScalar;
724 }
725 }
726 }
727 else
728 {
729 if ((uintptr_t)addr != (uintptr_t)m_data_buffer.GetBytes())
730 {
731 m_value.Clear();
732 m_value_type = eValueTypeScalar;
733 }
734 }
735 }
736 break;
737 }
738
739
740 }
741 return m_value;
742}
743
744Variable *
745Value::GetVariable()
746{
747 if (m_context_type == eContextTypeValue)
748 return ((Value*)m_context)->GetVariable();
749
Greg Clayton526e5af2010-11-13 03:52:47 +0000750 if (m_context_type == eContextTypeVariable)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000751 return static_cast<Variable *> (m_context);
752 return NULL;
753}
754
755
756
757const char *
758Value::GetValueTypeAsCString (ValueType value_type)
759{
760 switch (value_type)
761 {
762 case eValueTypeScalar: return "scalar";
763 case eValueTypeFileAddress: return "file address";
764 case eValueTypeLoadAddress: return "load address";
765 case eValueTypeHostAddress: return "host address";
766 };
767 return "???";
768}
769
770const char *
771Value::GetContextTypeAsCString (ContextType context_type)
772{
773 switch (context_type)
774 {
775 case eContextTypeInvalid: return "invalid";
Greg Clayton526e5af2010-11-13 03:52:47 +0000776 case eContextTypeClangType: return "clang::Type *";
777 case eContextTypeRegisterInfo: return "RegisterInfo *";
778 case eContextTypeLLDBType: return "Type *";
779 case eContextTypeVariable: return "Variable *";
Greg Claytonc982c762010-07-09 20:39:50 +0000780 case eContextTypeValue: return "Value"; // TODO: Sean, more description here?
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000781 };
782 return "???";
783}
784
785ValueList::ValueList (const ValueList &rhs)
786{
787 m_values = rhs.m_values;
788}
789
790const ValueList &
791ValueList::operator= (const ValueList &rhs)
792{
793 m_values = rhs.m_values;
794 return *this;
795}
796
797void
798ValueList::PushValue (const Value &value)
799{
800 m_values.push_back (value);
801}
802
803size_t
804ValueList::GetSize()
805{
806 return m_values.size();
807}
808
809Value *
810ValueList::GetValueAtIndex (size_t idx)
811{
812 if (idx < GetSize())
813 {
814 return &(m_values[idx]);
815 }
816 else
817 return NULL;
818}
819
820void
821ValueList::Clear ()
822{
823 m_values.clear();
824}