1 //===-- ValueObject.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/ValueObject.h"
11 
12 // C Includes
13 #include <stdlib.h>
14 
15 // C++ Includes
16 // Other libraries and framework includes
17 #include "llvm/Support/raw_ostream.h"
18 #include "clang/AST/Type.h"
19 
20 // Project includes
21 #include "lldb/Core/DataBufferHeap.h"
22 #include "lldb/Core/DataVisualization.h"
23 #include "lldb/Core/Debugger.h"
24 #include "lldb/Core/Log.h"
25 #include "lldb/Core/StreamString.h"
26 #include "lldb/Core/ValueObjectChild.h"
27 #include "lldb/Core/ValueObjectConstResult.h"
28 #include "lldb/Core/ValueObjectDynamicValue.h"
29 #include "lldb/Core/ValueObjectList.h"
30 #include "lldb/Core/ValueObjectMemory.h"
31 #include "lldb/Core/ValueObjectSyntheticFilter.h"
32 
33 #include "lldb/Host/Endian.h"
34 
35 #include "lldb/Interpreter/CommandInterpreter.h"
36 #include "lldb/Interpreter/ScriptInterpreterPython.h"
37 
38 #include "lldb/Symbol/ClangASTType.h"
39 #include "lldb/Symbol/ClangASTContext.h"
40 #include "lldb/Symbol/Type.h"
41 
42 #include "lldb/Target/ExecutionContext.h"
43 #include "lldb/Target/LanguageRuntime.h"
44 #include "lldb/Target/ObjCLanguageRuntime.h"
45 #include "lldb/Target/Process.h"
46 #include "lldb/Target/RegisterContext.h"
47 #include "lldb/Target/Target.h"
48 #include "lldb/Target/Thread.h"
49 
50 #include "lldb/Utility/RefCounter.h"
51 
52 using namespace lldb;
53 using namespace lldb_private;
54 using namespace lldb_utility;
55 
56 static user_id_t g_value_obj_uid = 0;
57 
58 //----------------------------------------------------------------------
59 // ValueObject constructor
60 //----------------------------------------------------------------------
61 ValueObject::ValueObject (ValueObject &parent) :
62     UserID (++g_value_obj_uid), // Unique identifier for every value object
63     m_parent (&parent),
64     m_update_point (parent.GetUpdatePoint ()),
65     m_name (),
66     m_data (),
67     m_value (),
68     m_error (),
69     m_value_str (),
70     m_old_value_str (),
71     m_location_str (),
72     m_summary_str (),
73     m_object_desc_str (),
74     m_manager(parent.GetManager()),
75     m_children (),
76     m_synthetic_children (),
77     m_dynamic_value (NULL),
78     m_synthetic_value(NULL),
79     m_deref_valobj(NULL),
80     m_format (eFormatDefault),
81     m_last_format_mgr_revision(0),
82     m_last_format_mgr_dynamic(parent.m_last_format_mgr_dynamic),
83     m_last_summary_format(),
84     m_forced_summary_format(),
85     m_last_value_format(),
86     m_last_synthetic_filter(),
87     m_user_id_of_forced_summary(),
88     m_value_is_valid (false),
89     m_value_did_change (false),
90     m_children_count_valid (false),
91     m_old_value_valid (false),
92     m_is_deref_of_parent (false),
93     m_is_array_item_for_pointer(false),
94     m_is_bitfield_for_scalar(false),
95     m_is_expression_path_child(false),
96     m_is_child_at_offset(false),
97     m_address_type_of_ptr_or_ref_children(eAddressTypeInvalid),
98     m_trying_summary_already(false)
99 {
100     m_manager->ManageObject(this);
101 }
102 
103 //----------------------------------------------------------------------
104 // ValueObject constructor
105 //----------------------------------------------------------------------
106 ValueObject::ValueObject (ExecutionContextScope *exe_scope,
107                           AddressType child_ptr_or_ref_addr_type) :
108     UserID (++g_value_obj_uid), // Unique identifier for every value object
109     m_parent (NULL),
110     m_update_point (exe_scope),
111     m_name (),
112     m_data (),
113     m_value (),
114     m_error (),
115     m_value_str (),
116     m_old_value_str (),
117     m_location_str (),
118     m_summary_str (),
119     m_object_desc_str (),
120     m_manager(),
121     m_children (),
122     m_synthetic_children (),
123     m_dynamic_value (NULL),
124     m_synthetic_value(NULL),
125     m_deref_valobj(NULL),
126     m_format (eFormatDefault),
127     m_last_format_mgr_revision(0),
128     m_last_format_mgr_dynamic(eNoDynamicValues),
129     m_last_summary_format(),
130     m_forced_summary_format(),
131     m_last_value_format(),
132     m_last_synthetic_filter(),
133     m_user_id_of_forced_summary(),
134     m_value_is_valid (false),
135     m_value_did_change (false),
136     m_children_count_valid (false),
137     m_old_value_valid (false),
138     m_is_deref_of_parent (false),
139     m_is_array_item_for_pointer(false),
140     m_is_bitfield_for_scalar(false),
141     m_is_expression_path_child(false),
142     m_is_child_at_offset(false),
143     m_address_type_of_ptr_or_ref_children(child_ptr_or_ref_addr_type),
144     m_trying_summary_already(false)
145 {
146     m_manager = new ValueObjectManager();
147     m_manager->ManageObject (this);
148 }
149 
150 //----------------------------------------------------------------------
151 // Destructor
152 //----------------------------------------------------------------------
153 ValueObject::~ValueObject ()
154 {
155 }
156 
157 bool
158 ValueObject::UpdateValueIfNeeded (bool update_format)
159 {
160     return UpdateValueIfNeeded(m_last_format_mgr_dynamic, update_format);
161 }
162 
163 bool
164 ValueObject::UpdateValueIfNeeded (DynamicValueType use_dynamic, bool update_format)
165 {
166 
167     bool did_change_formats = false;
168 
169     if (update_format)
170         did_change_formats = UpdateFormatsIfNeeded(use_dynamic);
171 
172     // If this is a constant value, then our success is predicated on whether
173     // we have an error or not
174     if (GetIsConstant())
175     {
176         // if you were asked to update your formatters, but did not get a chance to do it
177         // clear your own values (this serves the purpose of faking a stop-id for frozen
178         // objects (which are regarded as constant, but could have changes behind their backs
179         // because of the frozen-pointer depth limit)
180 		// TODO: decouple summary from value and then remove this code and only force-clear the summary
181         if (update_format && !did_change_formats)
182             m_summary_str.clear();
183         return m_error.Success();
184     }
185 
186     bool first_update = m_update_point.IsFirstEvaluation();
187 
188     if (m_update_point.NeedsUpdating())
189     {
190         m_update_point.SetUpdated();
191 
192         // Save the old value using swap to avoid a string copy which
193         // also will clear our m_value_str
194         if (m_value_str.empty())
195         {
196             m_old_value_valid = false;
197         }
198         else
199         {
200             m_old_value_valid = true;
201             m_old_value_str.swap (m_value_str);
202             m_value_str.clear();
203         }
204 
205         ClearUserVisibleData();
206 
207         const bool value_was_valid = GetValueIsValid();
208         SetValueDidChange (false);
209 
210         m_error.Clear();
211 
212         // Call the pure virtual function to update the value
213         bool success = UpdateValue ();
214 
215         SetValueIsValid (success);
216 
217         if (first_update)
218             SetValueDidChange (false);
219         else if (!m_value_did_change && success == false)
220         {
221             // The value wasn't gotten successfully, so we mark this
222             // as changed if the value used to be valid and now isn't
223             SetValueDidChange (value_was_valid);
224         }
225     }
226     return m_error.Success();
227 }
228 
229 bool
230 ValueObject::UpdateFormatsIfNeeded(DynamicValueType use_dynamic)
231 {
232     LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_TYPES));
233     if (log)
234         log->Printf("checking for FormatManager revisions. VO named %s is at revision %d, while the format manager is at revision %d",
235            GetName().GetCString(),
236            m_last_format_mgr_revision,
237            DataVisualization::GetCurrentRevision());
238 
239     bool any_change = false;
240 
241     if (HasCustomSummaryFormat() && m_update_point.GetModID() != m_user_id_of_forced_summary)
242     {
243         ClearCustomSummaryFormat();
244 
245         any_change = true;
246     }
247 
248     if ( (m_last_format_mgr_revision != DataVisualization::GetCurrentRevision()) ||
249           m_last_format_mgr_dynamic != use_dynamic)
250     {
251         SetValueFormat(DataVisualization::ValueFormats::GetFormat (*this, eNoDynamicValues));
252         SetSummaryFormat(DataVisualization::GetSummaryFormat (*this, use_dynamic));
253         SetSyntheticChildren(DataVisualization::GetSyntheticChildren (*this, use_dynamic));
254 
255         m_last_format_mgr_revision = DataVisualization::GetCurrentRevision();
256         m_last_format_mgr_dynamic = use_dynamic;
257 
258         any_change = true;
259     }
260 
261     return any_change;
262 
263 }
264 
265 void
266 ValueObject::SetNeedsUpdate ()
267 {
268     m_update_point.SetNeedsUpdate();
269     // We have to clear the value string here so ConstResult children will notice if their values are
270     // changed by hand (i.e. with SetValueAsCString).
271     m_value_str.clear();
272 }
273 
274 DataExtractor &
275 ValueObject::GetDataExtractor ()
276 {
277     UpdateValueIfNeeded(false);
278     return m_data;
279 }
280 
281 const Error &
282 ValueObject::GetError()
283 {
284     UpdateValueIfNeeded(false);
285     return m_error;
286 }
287 
288 const ConstString &
289 ValueObject::GetName() const
290 {
291     return m_name;
292 }
293 
294 const char *
295 ValueObject::GetLocationAsCString ()
296 {
297     if (UpdateValueIfNeeded(false))
298     {
299         if (m_location_str.empty())
300         {
301             StreamString sstr;
302 
303             switch (m_value.GetValueType())
304             {
305             default:
306                 break;
307 
308             case Value::eValueTypeScalar:
309                 if (m_value.GetContextType() == Value::eContextTypeRegisterInfo)
310                 {
311                     RegisterInfo *reg_info = m_value.GetRegisterInfo();
312                     if (reg_info)
313                     {
314                         if (reg_info->name)
315                             m_location_str = reg_info->name;
316                         else if (reg_info->alt_name)
317                             m_location_str = reg_info->alt_name;
318                         break;
319                     }
320                 }
321                 m_location_str = "scalar";
322                 break;
323 
324             case Value::eValueTypeLoadAddress:
325             case Value::eValueTypeFileAddress:
326             case Value::eValueTypeHostAddress:
327                 {
328                     uint32_t addr_nibble_size = m_data.GetAddressByteSize() * 2;
329                     sstr.Printf("0x%*.*llx", addr_nibble_size, addr_nibble_size, m_value.GetScalar().ULongLong(LLDB_INVALID_ADDRESS));
330                     m_location_str.swap(sstr.GetString());
331                 }
332                 break;
333             }
334         }
335     }
336     return m_location_str.c_str();
337 }
338 
339 Value &
340 ValueObject::GetValue()
341 {
342     return m_value;
343 }
344 
345 const Value &
346 ValueObject::GetValue() const
347 {
348     return m_value;
349 }
350 
351 bool
352 ValueObject::ResolveValue (Scalar &scalar)
353 {
354     if (UpdateValueIfNeeded(false)) // make sure that you are up to date before returning anything
355     {
356         ExecutionContext exe_ctx;
357         ExecutionContextScope *exe_scope = GetExecutionContextScope();
358         if (exe_scope)
359             exe_scope->CalculateExecutionContext(exe_ctx);
360         Value tmp_value(m_value);
361         scalar = tmp_value.ResolveValue(&exe_ctx, GetClangAST ());
362         return scalar.IsValid();
363     }
364     else
365         return false;
366 }
367 
368 bool
369 ValueObject::GetValueIsValid () const
370 {
371     return m_value_is_valid;
372 }
373 
374 
375 void
376 ValueObject::SetValueIsValid (bool b)
377 {
378     m_value_is_valid = b;
379 }
380 
381 bool
382 ValueObject::GetValueDidChange ()
383 {
384     GetValueAsCString ();
385     return m_value_did_change;
386 }
387 
388 void
389 ValueObject::SetValueDidChange (bool value_changed)
390 {
391     m_value_did_change = value_changed;
392 }
393 
394 ValueObjectSP
395 ValueObject::GetChildAtIndex (uint32_t idx, bool can_create)
396 {
397     ValueObjectSP child_sp;
398     // We may need to update our value if we are dynamic
399     if (IsPossibleDynamicType ())
400         UpdateValueIfNeeded(false);
401     if (idx < GetNumChildren())
402     {
403         // Check if we have already made the child value object?
404         if (can_create && m_children[idx] == NULL)
405         {
406             // No we haven't created the child at this index, so lets have our
407             // subclass do it and cache the result for quick future access.
408             m_children[idx] = CreateChildAtIndex (idx, false, 0);
409         }
410 
411         if (m_children[idx] != NULL)
412             return m_children[idx]->GetSP();
413     }
414     return child_sp;
415 }
416 
417 uint32_t
418 ValueObject::GetIndexOfChildWithName (const ConstString &name)
419 {
420     bool omit_empty_base_classes = true;
421     return ClangASTContext::GetIndexOfChildWithName (GetClangAST(),
422                                                      GetClangType(),
423                                                      name.GetCString(),
424                                                      omit_empty_base_classes);
425 }
426 
427 ValueObjectSP
428 ValueObject::GetChildMemberWithName (const ConstString &name, bool can_create)
429 {
430     // when getting a child by name, it could be buried inside some base
431     // classes (which really aren't part of the expression path), so we
432     // need a vector of indexes that can get us down to the correct child
433     ValueObjectSP child_sp;
434 
435     // We may need to update our value if we are dynamic
436     if (IsPossibleDynamicType ())
437         UpdateValueIfNeeded(false);
438 
439     std::vector<uint32_t> child_indexes;
440     clang::ASTContext *clang_ast = GetClangAST();
441     void *clang_type = GetClangType();
442     bool omit_empty_base_classes = true;
443     const size_t num_child_indexes =  ClangASTContext::GetIndexOfChildMemberWithName (clang_ast,
444                                                                                       clang_type,
445                                                                                       name.GetCString(),
446                                                                                       omit_empty_base_classes,
447                                                                                       child_indexes);
448     if (num_child_indexes > 0)
449     {
450         std::vector<uint32_t>::const_iterator pos = child_indexes.begin ();
451         std::vector<uint32_t>::const_iterator end = child_indexes.end ();
452 
453         child_sp = GetChildAtIndex(*pos, can_create);
454         for (++pos; pos != end; ++pos)
455         {
456             if (child_sp)
457             {
458                 ValueObjectSP new_child_sp(child_sp->GetChildAtIndex (*pos, can_create));
459                 child_sp = new_child_sp;
460             }
461             else
462             {
463                 child_sp.reset();
464             }
465 
466         }
467     }
468     return child_sp;
469 }
470 
471 
472 uint32_t
473 ValueObject::GetNumChildren ()
474 {
475     if (!m_children_count_valid)
476     {
477         SetNumChildren (CalculateNumChildren());
478     }
479     return m_children.size();
480 }
481 void
482 ValueObject::SetNumChildren (uint32_t num_children)
483 {
484     m_children_count_valid = true;
485     m_children.resize(num_children);
486 }
487 
488 void
489 ValueObject::SetName (const ConstString &name)
490 {
491     m_name = name;
492 }
493 
494 ValueObject *
495 ValueObject::CreateChildAtIndex (uint32_t idx, bool synthetic_array_member, int32_t synthetic_index)
496 {
497     ValueObject *valobj = NULL;
498 
499     bool omit_empty_base_classes = true;
500     bool ignore_array_bounds = synthetic_array_member;
501     std::string child_name_str;
502     uint32_t child_byte_size = 0;
503     int32_t child_byte_offset = 0;
504     uint32_t child_bitfield_bit_size = 0;
505     uint32_t child_bitfield_bit_offset = 0;
506     bool child_is_base_class = false;
507     bool child_is_deref_of_parent = false;
508 
509     const bool transparent_pointers = synthetic_array_member == false;
510     clang::ASTContext *clang_ast = GetClangAST();
511     clang_type_t clang_type = GetClangType();
512     clang_type_t child_clang_type;
513 
514     ExecutionContext exe_ctx;
515     GetExecutionContextScope()->CalculateExecutionContext (exe_ctx);
516 
517     child_clang_type = ClangASTContext::GetChildClangTypeAtIndex (&exe_ctx,
518                                                                   clang_ast,
519                                                                   GetName().GetCString(),
520                                                                   clang_type,
521                                                                   idx,
522                                                                   transparent_pointers,
523                                                                   omit_empty_base_classes,
524                                                                   ignore_array_bounds,
525                                                                   child_name_str,
526                                                                   child_byte_size,
527                                                                   child_byte_offset,
528                                                                   child_bitfield_bit_size,
529                                                                   child_bitfield_bit_offset,
530                                                                   child_is_base_class,
531                                                                   child_is_deref_of_parent);
532     if (child_clang_type && child_byte_size)
533     {
534         if (synthetic_index)
535             child_byte_offset += child_byte_size * synthetic_index;
536 
537         ConstString child_name;
538         if (!child_name_str.empty())
539             child_name.SetCString (child_name_str.c_str());
540 
541         valobj = new ValueObjectChild (*this,
542                                        clang_ast,
543                                        child_clang_type,
544                                        child_name,
545                                        child_byte_size,
546                                        child_byte_offset,
547                                        child_bitfield_bit_size,
548                                        child_bitfield_bit_offset,
549                                        child_is_base_class,
550                                        child_is_deref_of_parent,
551                                        eAddressTypeInvalid);
552         //if (valobj)
553         //    valobj->SetAddressTypeOfChildren(eAddressTypeInvalid);
554    }
555 
556     return valobj;
557 }
558 
559 const char *
560 ValueObject::GetSummaryAsCString ()
561 {
562     if (UpdateValueIfNeeded (true))
563     {
564         if (m_summary_str.empty())
565         {
566             SummaryFormat *summary_format = GetSummaryFormat().get();
567 
568             if (summary_format)
569             {
570                 m_summary_str = summary_format->FormatObject(GetSP());
571             }
572             else
573             {
574                 clang_type_t clang_type = GetClangType();
575 
576                 // Do some default printout for function pointers
577                 if (clang_type)
578                 {
579                     StreamString sstr;
580                     clang_type_t elem_or_pointee_clang_type;
581                     const Flags type_flags (ClangASTContext::GetTypeInfo (clang_type,
582                                                                           GetClangAST(),
583                                                                           &elem_or_pointee_clang_type));
584 
585                     ExecutionContextScope *exe_scope = GetExecutionContextScope();
586                     if (exe_scope)
587                     {
588                         if (ClangASTContext::IsFunctionPointerType (clang_type))
589                         {
590                             AddressType func_ptr_address_type = eAddressTypeInvalid;
591                             addr_t func_ptr_address = GetPointerValue (&func_ptr_address_type);
592                             if (func_ptr_address != 0 && func_ptr_address != LLDB_INVALID_ADDRESS)
593                             {
594                                 switch (func_ptr_address_type)
595                                 {
596                                 case eAddressTypeInvalid:
597                                 case eAddressTypeFile:
598                                     break;
599 
600                                 case eAddressTypeLoad:
601                                     {
602                                         Address so_addr;
603                                         Target *target = exe_scope->CalculateTarget();
604                                         if (target && target->GetSectionLoadList().IsEmpty() == false)
605                                         {
606                                             if (target->GetSectionLoadList().ResolveLoadAddress(func_ptr_address, so_addr))
607                                             {
608                                                 so_addr.Dump (&sstr,
609                                                               exe_scope,
610                                                               Address::DumpStyleResolvedDescription,
611                                                               Address::DumpStyleSectionNameOffset);
612                                             }
613                                         }
614                                     }
615                                     break;
616 
617                                 case eAddressTypeHost:
618                                     break;
619                                 }
620                             }
621                             if (sstr.GetSize() > 0)
622                             {
623                                 m_summary_str.assign (1, '(');
624                                 m_summary_str.append (sstr.GetData(), sstr.GetSize());
625                                 m_summary_str.append (1, ')');
626                             }
627                         }
628                     }
629                 }
630             }
631         }
632     }
633     if (m_summary_str.empty())
634         return NULL;
635     return m_summary_str.c_str();
636 }
637 
638 bool
639 ValueObject::IsCStringContainer(bool check_pointer)
640 {
641     clang_type_t elem_or_pointee_clang_type;
642     const Flags type_flags (ClangASTContext::GetTypeInfo (GetClangType(),
643                                                           GetClangAST(),
644                                                           &elem_or_pointee_clang_type));
645     bool is_char_arr_ptr (type_flags.AnySet (ClangASTContext::eTypeIsArray | ClangASTContext::eTypeIsPointer) &&
646             ClangASTContext::IsCharType (elem_or_pointee_clang_type));
647     if (!is_char_arr_ptr)
648         return false;
649     if (!check_pointer)
650         return true;
651     if (type_flags.Test(ClangASTContext::eTypeIsArray))
652         return true;
653     addr_t cstr_address = LLDB_INVALID_ADDRESS;
654     AddressType cstr_address_type = eAddressTypeInvalid;
655     cstr_address = GetAddressOf (true, &cstr_address_type);
656     return (cstr_address != LLDB_INVALID_ADDRESS);
657 }
658 
659 size_t
660 ValueObject::GetPointeeData (DataExtractor& data,
661                              uint32_t item_idx,
662                              uint32_t item_count)
663 {
664     if (!IsPointerType() && !IsArrayType())
665         return 0;
666 
667     if (item_count == 0)
668         return 0;
669 
670     uint32_t stride = 0;
671 
672     ClangASTType type(GetClangAST(),
673                       GetClangType());
674 
675     const uint64_t item_type_size = (IsPointerType() ? ClangASTType::GetTypeByteSize(GetClangAST(), type.GetPointeeType()) :
676                                      ClangASTType::GetTypeByteSize(GetClangAST(), type.GetArrayElementType(stride)));
677 
678     const uint64_t bytes = item_count * item_type_size;
679 
680     const uint64_t offset = item_idx * item_type_size;
681 
682     if (item_idx == 0 && item_count == 1) // simply a deref
683     {
684         if (IsPointerType())
685         {
686             Error error;
687             ValueObjectSP pointee_sp = Dereference(error);
688             if (error.Fail() || pointee_sp.get() == NULL)
689                 return 0;
690             return pointee_sp->GetDataExtractor().Copy(data);
691         }
692         else
693         {
694             ValueObjectSP child_sp = GetChildAtIndex(0, true);
695             if (child_sp.get() == NULL)
696                 return 0;
697             return child_sp->GetDataExtractor().Copy(data);
698         }
699         return true;
700     }
701     else /* (items > 1) */
702     {
703         Error error;
704         lldb_private::DataBufferHeap* heap_buf_ptr = NULL;
705         lldb::DataBufferSP data_sp(heap_buf_ptr = new lldb_private::DataBufferHeap());
706 
707         AddressType addr_type;
708         lldb::addr_t addr = IsPointerType() ? GetPointerValue(&addr_type) : GetAddressOf(true, &addr_type);
709 
710         ExecutionContextScope *exe_scope = m_update_point.GetExecutionContextScope();
711 
712 
713         switch (addr_type)
714         {
715             case eAddressTypeFile:
716                 {
717                     Module* module = GetModule();
718                     if (module)
719                     {
720                         Address so_addr;
721                         module->ResolveFileAddress(addr, so_addr);
722                         if (exe_scope)
723                         {
724                             Target* target = exe_scope->CalculateTarget();
725                             if (target)
726                             {
727                                 heap_buf_ptr->SetByteSize(bytes);
728                                 size_t bytes_read = target->ReadMemory(so_addr, false, heap_buf_ptr->GetBytes(), bytes, error);
729                                 if (error.Success())
730                                 {
731                                     data.SetData(data_sp);
732                                     return bytes_read;
733                                 }
734                             }
735                         }
736                     }
737                 }
738                 break;
739             case eAddressTypeLoad:
740                 if (exe_scope)
741                 {
742                     Process *process = exe_scope->CalculateProcess();
743                     if (process)
744                     {
745                         heap_buf_ptr->SetByteSize(bytes);
746                         size_t bytes_read = process->ReadMemory(addr + offset, heap_buf_ptr->GetBytes(), bytes, error);
747                         if (error.Success())
748                         {
749                             data.SetData(data_sp);
750                             return bytes_read;
751                         }
752                     }
753                 }
754                 break;
755             case eAddressTypeHost:
756                 {
757                     heap_buf_ptr->CopyData((uint8_t*)(addr + offset), bytes);
758                     data.SetData(data_sp);
759                     return bytes;
760                 }
761                 break;
762             case eAddressTypeInvalid:
763             default:
764                 break;
765         }
766     }
767     return 0;
768 }
769 
770 size_t
771 ValueObject::GetData (DataExtractor& data)
772 {
773     UpdateValueIfNeeded(false);
774     ExecutionContext exe_ctx;
775     GetExecutionContextScope()->CalculateExecutionContext(exe_ctx);
776     Error error = m_value.GetValueAsData(&exe_ctx, GetClangAST(), data, 0, GetModule());
777     if (error.Fail())
778         return 0;
779     data.SetAddressByteSize(m_data.GetAddressByteSize());
780     data.SetByteOrder(m_data.GetByteOrder());
781     return data.GetByteSize();
782 }
783 
784 // will compute strlen(str), but without consuming more than
785 // maxlen bytes out of str (this serves the purpose of reading
786 // chunks of a string without having to worry about
787 // missing NULL terminators in the chunk)
788 // of course, if strlen(str) > maxlen, the function will return
789 // maxlen_value (which should be != maxlen, because that allows you
790 // to know whether strlen(str) == maxlen or strlen(str) > maxlen)
791 static uint32_t
792 strlen_or_inf (const char* str,
793                uint32_t maxlen,
794                uint32_t maxlen_value)
795 {
796     uint32_t len = 0;
797     if (str)
798     {
799         while(*str)
800         {
801             len++;str++;
802             if (len > maxlen)
803                 return maxlen_value;
804         }
805     }
806     return len;
807 }
808 
809 void
810 ValueObject::ReadPointedString(Stream& s,
811                                Error& error,
812                                uint32_t max_length,
813                                bool honor_array,
814                                Format item_format)
815 {
816 
817     if (max_length == 0)
818         max_length = GetUpdatePoint().GetTargetSP()->GetMaximumSizeOfStringSummary();
819 
820     clang_type_t clang_type = GetClangType();
821     clang_type_t elem_or_pointee_clang_type;
822     const Flags type_flags (ClangASTContext::GetTypeInfo (clang_type,
823                                                           GetClangAST(),
824                                                           &elem_or_pointee_clang_type));
825     if (type_flags.AnySet (ClangASTContext::eTypeIsArray | ClangASTContext::eTypeIsPointer) &&
826         ClangASTContext::IsCharType (elem_or_pointee_clang_type))
827     {
828         ExecutionContextScope *exe_scope = GetExecutionContextScope();
829             if (exe_scope)
830             {
831                 Target *target = exe_scope->CalculateTarget();
832                 if (target == NULL)
833                 {
834                     s << "<no target to read from>";
835                 }
836                 else
837                 {
838                     addr_t cstr_address = LLDB_INVALID_ADDRESS;
839                     AddressType cstr_address_type = eAddressTypeInvalid;
840 
841                     size_t cstr_len = 0;
842                     bool capped_data = false;
843                     if (type_flags.Test (ClangASTContext::eTypeIsArray))
844                     {
845                         // We have an array
846                         cstr_len = ClangASTContext::GetArraySize (clang_type);
847                         if (cstr_len > max_length)
848                         {
849                             capped_data = true;
850                             cstr_len = max_length;
851                         }
852                         cstr_address = GetAddressOf (true, &cstr_address_type);
853                     }
854                     else
855                     {
856                         // We have a pointer
857                         cstr_address = GetPointerValue (&cstr_address_type);
858                     }
859                     if (cstr_address != 0 && cstr_address != LLDB_INVALID_ADDRESS)
860                     {
861                         Address cstr_so_addr (NULL, cstr_address);
862                         DataExtractor data;
863                         size_t bytes_read = 0;
864                         if (cstr_len > 0 && honor_array)
865                         {
866                             // I am using GetPointeeData() here to abstract the fact that some ValueObjects are actually frozen pointers in the host
867 						    // but the pointed-to data lives in the debuggee, and GetPointeeData() automatically takes care of this
868                             GetPointeeData(data, 0, cstr_len);
869 
870                             if ((bytes_read = data.GetByteSize()) > 0)
871                             {
872                                 s << '"';
873                                 data.Dump (&s,
874                                            0,                 // Start offset in "data"
875                                            item_format,
876                                            1,                 // Size of item (1 byte for a char!)
877                                            bytes_read,        // How many bytes to print?
878                                            UINT32_MAX,        // num per line
879                                            LLDB_INVALID_ADDRESS,// base address
880                                            0,                 // bitfield bit size
881                                            0);                // bitfield bit offset
882                                 if (capped_data)
883                                     s << "...";
884                                 s << '"';
885                             }
886                         }
887                         else
888                         {
889                             cstr_len = max_length;
890                             const size_t k_max_buf_size = 64;
891 
892                             size_t offset = 0;
893 
894                             int cstr_len_displayed = -1;
895                             bool capped_cstr = false;
896 							// I am using GetPointeeData() here to abstract the fact that some ValueObjects are actually frozen pointers in the host
897 						    // but the pointed-to data lives in the debuggee, and GetPointeeData() automatically takes care of this
898                             while ((bytes_read = GetPointeeData(data, offset, k_max_buf_size)) > 0)
899                             {
900                                 const char *cstr = data.PeekCStr(0);
901                                 size_t len = strlen_or_inf (cstr, k_max_buf_size, k_max_buf_size+1);
902                                 if (len > k_max_buf_size)
903                                     len = k_max_buf_size;
904                                 if (cstr && cstr_len_displayed < 0)
905                                     s << '"';
906 
907                                 if (cstr_len_displayed < 0)
908                                     cstr_len_displayed = len;
909 
910                                 if (len == 0)
911                                     break;
912                                 cstr_len_displayed += len;
913                                 if (len > bytes_read)
914                                     len = bytes_read;
915                                 if (len > cstr_len)
916                                     len = cstr_len;
917 
918                                 data.Dump (&s,
919                                            0,                 // Start offset in "data"
920                                            item_format,
921                                            1,                 // Size of item (1 byte for a char!)
922                                            len,               // How many bytes to print?
923                                            UINT32_MAX,        // num per line
924                                            LLDB_INVALID_ADDRESS,// base address
925                                            0,                 // bitfield bit size
926                                            0);                // bitfield bit offset
927 
928                                 if (len < k_max_buf_size)
929                                     break;
930 
931                                 if (len >= cstr_len)
932                                 {
933                                     capped_cstr = true;
934                                     break;
935                                 }
936 
937                                 cstr_len -= len;
938                                 offset += len;
939                             }
940 
941                             if (cstr_len_displayed >= 0)
942                             {
943                                 s << '"';
944                                 if (capped_cstr)
945                                     s << "...";
946                             }
947                         }
948                     }
949                 }
950             }
951     }
952     else
953     {
954         error.SetErrorString("impossible to read a string from this object");
955         s << "<not a string object>";
956     }
957 }
958 
959 const char *
960 ValueObject::GetObjectDescription ()
961 {
962 
963     if (!UpdateValueIfNeeded (true))
964         return NULL;
965 
966     if (!m_object_desc_str.empty())
967         return m_object_desc_str.c_str();
968 
969     ExecutionContextScope *exe_scope = GetExecutionContextScope();
970     if (exe_scope == NULL)
971         return NULL;
972 
973     Process *process = exe_scope->CalculateProcess();
974     if (process == NULL)
975         return NULL;
976 
977     StreamString s;
978 
979     LanguageType language = GetObjectRuntimeLanguage();
980     LanguageRuntime *runtime = process->GetLanguageRuntime(language);
981 
982     if (runtime == NULL)
983     {
984         // Aw, hell, if the things a pointer, or even just an integer, let's try ObjC anyway...
985         clang_type_t opaque_qual_type = GetClangType();
986         if (opaque_qual_type != NULL)
987         {
988             bool is_signed;
989             if (ClangASTContext::IsIntegerType (opaque_qual_type, is_signed)
990                 || ClangASTContext::IsPointerType (opaque_qual_type))
991             {
992                 runtime = process->GetLanguageRuntime(eLanguageTypeObjC);
993             }
994         }
995     }
996 
997     if (runtime && runtime->GetObjectDescription(s, *this))
998     {
999         m_object_desc_str.append (s.GetData());
1000     }
1001 
1002     if (m_object_desc_str.empty())
1003         return NULL;
1004     else
1005         return m_object_desc_str.c_str();
1006 }
1007 
1008 const char *
1009 ValueObject::GetValueAsCString ()
1010 {
1011     // If our byte size is zero this is an aggregate type that has children
1012     if (ClangASTContext::IsAggregateType (GetClangType()) == false)
1013     {
1014         if (UpdateValueIfNeeded(true))
1015         {
1016             if (m_value_str.empty())
1017             {
1018                 const Value::ContextType context_type = m_value.GetContextType();
1019 
1020                 switch (context_type)
1021                 {
1022                 case Value::eContextTypeClangType:
1023                 case Value::eContextTypeLLDBType:
1024                 case Value::eContextTypeVariable:
1025                     {
1026                         lldb::Format my_format = GetFormat();
1027                         clang_type_t clang_type = GetClangType ();
1028                         if (clang_type)
1029                         {
1030                             if (m_format == lldb::eFormatDefault)
1031                             {
1032                                 if (m_last_value_format)
1033                                     my_format = m_last_value_format->GetFormat();
1034                                 else
1035                                 {
1036                                     if (m_is_bitfield_for_scalar)
1037                                         my_format = eFormatUnsigned;
1038                                     else
1039                                         my_format = ClangASTType::GetFormat(clang_type);
1040                                 }
1041                             }
1042                             StreamString sstr;
1043                             if (ClangASTType::DumpTypeValue (GetClangAST(),            // The clang AST
1044                                                              clang_type,               // The clang type to display
1045                                                              &sstr,
1046                                                              my_format,                   // Format to display this type with
1047                                                              m_data,                   // Data to extract from
1048                                                              0,                        // Byte offset into "m_data"
1049                                                              GetByteSize(),            // Byte size of item in "m_data"
1050                                                              GetBitfieldBitSize(),     // Bitfield bit size
1051                                                              GetBitfieldBitOffset()))  // Bitfield bit offset
1052                                 m_value_str.swap(sstr.GetString());
1053                             else
1054                             {
1055                                 m_error.SetErrorStringWithFormat ("unsufficient data for value (only %lu of %lu bytes available)",
1056                                                                   m_data.GetByteSize(),
1057                                                                   GetByteSize());
1058                                 m_value_str.clear();
1059                             }
1060                         }
1061                     }
1062                     break;
1063 
1064                 case Value::eContextTypeRegisterInfo:
1065                     {
1066                         const RegisterInfo *reg_info = m_value.GetRegisterInfo();
1067                         if (reg_info)
1068                         {
1069                             StreamString reg_sstr;
1070                             m_data.Dump(&reg_sstr, 0, reg_info->format, reg_info->byte_size, 1, UINT32_MAX, LLDB_INVALID_ADDRESS, 0, 0);
1071                             m_value_str.swap(reg_sstr.GetString());
1072                         }
1073                     }
1074                     break;
1075 
1076                 default:
1077                     break;
1078                 }
1079             }
1080 
1081             if (!m_value_did_change && m_old_value_valid)
1082             {
1083                 // The value was gotten successfully, so we consider the
1084                 // value as changed if the value string differs
1085                 SetValueDidChange (m_old_value_str != m_value_str);
1086             }
1087         }
1088     }
1089     if (m_value_str.empty())
1090         return NULL;
1091     return m_value_str.c_str();
1092 }
1093 
1094 // if > 8bytes, 0 is returned. this method should mostly be used
1095 // to read address values out of pointers
1096 uint64_t
1097 ValueObject::GetValueAsUnsigned (uint64_t fail_value)
1098 {
1099     // If our byte size is zero this is an aggregate type that has children
1100     if (ClangASTContext::IsAggregateType (GetClangType()) == false)
1101     {
1102         Scalar scalar;
1103         if (ResolveValue (scalar))
1104             return scalar.GetRawBits64(fail_value);
1105     }
1106     return fail_value;
1107 }
1108 
1109 bool
1110 ValueObject::GetPrintableRepresentation(Stream& s,
1111                                         ValueObjectRepresentationStyle val_obj_display,
1112                                         Format custom_format)
1113 {
1114 
1115     if (custom_format != eFormatInvalid)
1116         SetFormat(custom_format);
1117 
1118     const char * return_value;
1119     std::string alloc_mem;
1120 
1121     switch(val_obj_display)
1122     {
1123         case eDisplayValue:
1124             return_value = GetValueAsCString();
1125             break;
1126         case eDisplaySummary:
1127             if (m_trying_summary_already)
1128                 return_value = NULL;
1129             else
1130             {
1131                 m_trying_summary_already = true;
1132                 return_value = GetSummaryAsCString();
1133                 m_trying_summary_already = false;
1134                 break;
1135             }
1136         case eDisplayLanguageSpecific:
1137             return_value = GetObjectDescription();
1138             break;
1139         case eDisplayLocation:
1140             return_value = GetLocationAsCString();
1141             break;
1142         case eDisplayChildrenCount:
1143         {
1144             alloc_mem.resize(512);
1145             return_value = &alloc_mem[0];
1146             int count = GetNumChildren();
1147             snprintf((char*)return_value, 512, "%d", count);
1148             break;
1149         }
1150         case eDisplayType:
1151             return_value = GetTypeName().AsCString();
1152             break;
1153         default:
1154             break;
1155     }
1156 
1157     if (!return_value)
1158     {
1159         if (val_obj_display == eDisplayValue)
1160             return_value = GetSummaryAsCString();
1161         else if (val_obj_display == eDisplaySummary)
1162         {
1163             if (ClangASTContext::IsAggregateType (GetClangType()) == true)
1164             {
1165                 // this thing has no value, and it seems to have no summary
1166                 // some combination of unitialized data and other factors can also
1167                 // raise this condition, so let's print a nice generic description
1168                 {
1169                     alloc_mem.resize(684);
1170                     return_value = &alloc_mem[0];
1171                     snprintf((char*)return_value, 684, "%s @ %s", GetTypeName().AsCString(), GetLocationAsCString());
1172                 }
1173             }
1174             else
1175                 return_value = GetValueAsCString();
1176         }
1177     }
1178 
1179     if (return_value)
1180         s.PutCString(return_value);
1181     else
1182     {
1183         if (m_error.Fail())
1184             s.Printf("<%s>", m_error.AsCString());
1185         else if (val_obj_display == eDisplaySummary)
1186             s.PutCString("<no summary available>");
1187         else if (val_obj_display == eDisplayValue)
1188             s.PutCString("<no value available>");
1189         else if (val_obj_display == eDisplayLanguageSpecific)
1190             s.PutCString("<not a valid Objective-C object>"); // edit this if we have other runtimes that support a description
1191         else
1192             s.PutCString("<no printable representation>");
1193     }
1194 
1195     // we should only return false here if we could not do *anything*
1196     // even if we have an error message as output, that's a success
1197     // from our callers' perspective, so return true
1198     return true;
1199 
1200 }
1201 
1202 // if any more "special cases" are added to ValueObject::DumpPrintableRepresentation() please keep
1203 // this call up to date by returning true for your new special cases. We will eventually move
1204 // to checking this call result before trying to display special cases
1205 bool
1206 ValueObject::HasSpecialCasesForPrintableRepresentation(ValueObjectRepresentationStyle val_obj_display,
1207                                                        Format custom_format)
1208 {
1209     clang_type_t elem_or_pointee_type;
1210     Flags flags(ClangASTContext::GetTypeInfo(GetClangType(), GetClangAST(), &elem_or_pointee_type));
1211 
1212     if (flags.AnySet(ClangASTContext::eTypeIsArray | ClangASTContext::eTypeIsPointer)
1213         && val_obj_display == ValueObject::eDisplayValue)
1214     {
1215         if (IsCStringContainer(true) &&
1216             (custom_format == eFormatCString ||
1217              custom_format == eFormatCharArray ||
1218              custom_format == eFormatChar ||
1219              custom_format == eFormatVectorOfChar))
1220             return true;
1221 
1222         if (flags.Test(ClangASTContext::eTypeIsArray))
1223         {
1224             if ((custom_format == eFormatBytes) ||
1225                 (custom_format == eFormatBytesWithASCII))
1226                 return true;
1227 
1228             if ((custom_format == eFormatVectorOfChar) ||
1229                 (custom_format == eFormatVectorOfFloat32) ||
1230                 (custom_format == eFormatVectorOfFloat64) ||
1231                 (custom_format == eFormatVectorOfSInt16) ||
1232                 (custom_format == eFormatVectorOfSInt32) ||
1233                 (custom_format == eFormatVectorOfSInt64) ||
1234                 (custom_format == eFormatVectorOfSInt8) ||
1235                 (custom_format == eFormatVectorOfUInt128) ||
1236                 (custom_format == eFormatVectorOfUInt16) ||
1237                 (custom_format == eFormatVectorOfUInt32) ||
1238                 (custom_format == eFormatVectorOfUInt64) ||
1239                 (custom_format == eFormatVectorOfUInt8))
1240                 return true;
1241         }
1242     }
1243     return false;
1244 }
1245 
1246 bool
1247 ValueObject::DumpPrintableRepresentation(Stream& s,
1248                                          ValueObjectRepresentationStyle val_obj_display,
1249                                          Format custom_format,
1250                                          bool only_special)
1251 {
1252 
1253     clang_type_t elem_or_pointee_type;
1254     Flags flags(ClangASTContext::GetTypeInfo(GetClangType(), GetClangAST(), &elem_or_pointee_type));
1255 
1256     if (flags.AnySet(ClangASTContext::eTypeIsArray | ClangASTContext::eTypeIsPointer)
1257          && val_obj_display == ValueObject::eDisplayValue)
1258     {
1259         // when being asked to get a printable display an array or pointer type directly,
1260         // try to "do the right thing"
1261 
1262         if (IsCStringContainer(true) &&
1263             (custom_format == eFormatCString ||
1264              custom_format == eFormatCharArray ||
1265              custom_format == eFormatChar ||
1266              custom_format == eFormatVectorOfChar)) // print char[] & char* directly
1267         {
1268             Error error;
1269             ReadPointedString(s,
1270                               error,
1271                               0,
1272                               (custom_format == eFormatVectorOfChar) ||
1273                               (custom_format == eFormatCharArray));
1274             return !error.Fail();
1275         }
1276 
1277         if (custom_format == eFormatEnum)
1278             return false;
1279 
1280         // this only works for arrays, because I have no way to know when
1281         // the pointed memory ends, and no special \0 end of data marker
1282         if (flags.Test(ClangASTContext::eTypeIsArray))
1283         {
1284             if ((custom_format == eFormatBytes) ||
1285                 (custom_format == eFormatBytesWithASCII))
1286             {
1287                 uint32_t count = GetNumChildren();
1288 
1289                 s << '[';
1290                 for (uint32_t low = 0; low < count; low++)
1291                 {
1292 
1293                     if (low)
1294                         s << ',';
1295 
1296                     ValueObjectSP child = GetChildAtIndex(low,true);
1297                     if (!child.get())
1298                     {
1299                         s << "<invalid child>";
1300                         continue;
1301                     }
1302                     child->DumpPrintableRepresentation(s, ValueObject::eDisplayValue, custom_format);
1303                 }
1304 
1305                 s << ']';
1306 
1307                 return true;
1308             }
1309 
1310             if ((custom_format == eFormatVectorOfChar) ||
1311                 (custom_format == eFormatVectorOfFloat32) ||
1312                 (custom_format == eFormatVectorOfFloat64) ||
1313                 (custom_format == eFormatVectorOfSInt16) ||
1314                 (custom_format == eFormatVectorOfSInt32) ||
1315                 (custom_format == eFormatVectorOfSInt64) ||
1316                 (custom_format == eFormatVectorOfSInt8) ||
1317                 (custom_format == eFormatVectorOfUInt128) ||
1318                 (custom_format == eFormatVectorOfUInt16) ||
1319                 (custom_format == eFormatVectorOfUInt32) ||
1320                 (custom_format == eFormatVectorOfUInt64) ||
1321                 (custom_format == eFormatVectorOfUInt8)) // arrays of bytes, bytes with ASCII or any vector format should be printed directly
1322             {
1323                 uint32_t count = GetNumChildren();
1324 
1325                 Format format = FormatManager::GetSingleItemFormat(custom_format);
1326 
1327                 s << '[';
1328                 for (uint32_t low = 0; low < count; low++)
1329                 {
1330 
1331                     if (low)
1332                         s << ',';
1333 
1334                     ValueObjectSP child = GetChildAtIndex(low,true);
1335                     if (!child.get())
1336                     {
1337                         s << "<invalid child>";
1338                         continue;
1339                     }
1340                     child->DumpPrintableRepresentation(s, ValueObject::eDisplayValue, format);
1341                 }
1342 
1343                 s << ']';
1344 
1345                 return true;
1346             }
1347         }
1348 
1349         if ((custom_format == eFormatBoolean) ||
1350             (custom_format == eFormatBinary) ||
1351             (custom_format == eFormatChar) ||
1352             (custom_format == eFormatCharPrintable) ||
1353             (custom_format == eFormatComplexFloat) ||
1354             (custom_format == eFormatDecimal) ||
1355             (custom_format == eFormatHex) ||
1356             (custom_format == eFormatFloat) ||
1357             (custom_format == eFormatOctal) ||
1358             (custom_format == eFormatOSType) ||
1359             (custom_format == eFormatUnicode16) ||
1360             (custom_format == eFormatUnicode32) ||
1361             (custom_format == eFormatUnsigned) ||
1362             (custom_format == eFormatPointer) ||
1363             (custom_format == eFormatComplexInteger) ||
1364             (custom_format == eFormatComplex) ||
1365             (custom_format == eFormatDefault)) // use the [] operator
1366             return false;
1367     }
1368 
1369     if (only_special)
1370         return false;
1371 
1372     bool var_success = GetPrintableRepresentation(s, val_obj_display, custom_format);
1373     if (custom_format != eFormatInvalid)
1374         SetFormat(eFormatDefault);
1375     return var_success;
1376 }
1377 
1378 addr_t
1379 ValueObject::GetAddressOf (bool scalar_is_load_address, AddressType *address_type)
1380 {
1381     if (!UpdateValueIfNeeded(false))
1382         return LLDB_INVALID_ADDRESS;
1383 
1384     switch (m_value.GetValueType())
1385     {
1386     case Value::eValueTypeScalar:
1387         if (scalar_is_load_address)
1388         {
1389             if(address_type)
1390                 *address_type = eAddressTypeLoad;
1391             return m_value.GetScalar().ULongLong(LLDB_INVALID_ADDRESS);
1392         }
1393         break;
1394 
1395     case Value::eValueTypeLoadAddress:
1396     case Value::eValueTypeFileAddress:
1397     case Value::eValueTypeHostAddress:
1398         {
1399             if(address_type)
1400                 *address_type = m_value.GetValueAddressType ();
1401             return m_value.GetScalar().ULongLong(LLDB_INVALID_ADDRESS);
1402         }
1403         break;
1404     }
1405     if (address_type)
1406         *address_type = eAddressTypeInvalid;
1407     return LLDB_INVALID_ADDRESS;
1408 }
1409 
1410 addr_t
1411 ValueObject::GetPointerValue (AddressType *address_type)
1412 {
1413     addr_t address = LLDB_INVALID_ADDRESS;
1414     if(address_type)
1415         *address_type = eAddressTypeInvalid;
1416 
1417     if (!UpdateValueIfNeeded(false))
1418         return address;
1419 
1420     switch (m_value.GetValueType())
1421     {
1422     case Value::eValueTypeScalar:
1423         address = m_value.GetScalar().ULongLong(LLDB_INVALID_ADDRESS);
1424         break;
1425 
1426     case Value::eValueTypeHostAddress:
1427     case Value::eValueTypeLoadAddress:
1428     case Value::eValueTypeFileAddress:
1429         {
1430             uint32_t data_offset = 0;
1431             address = m_data.GetPointer(&data_offset);
1432         }
1433         break;
1434     }
1435 
1436     if (address_type)
1437         *address_type = GetAddressTypeOfChildren();
1438 
1439     return address;
1440 }
1441 
1442 bool
1443 ValueObject::SetValueFromCString (const char *value_str)
1444 {
1445     // Make sure our value is up to date first so that our location and location
1446     // type is valid.
1447     if (!UpdateValueIfNeeded(false))
1448         return false;
1449 
1450     uint32_t count = 0;
1451     Encoding encoding = ClangASTType::GetEncoding (GetClangType(), count);
1452 
1453     const size_t byte_size = GetByteSize();
1454 
1455     Value::ValueType value_type = m_value.GetValueType();
1456 
1457     if (value_type == Value::eValueTypeScalar)
1458     {
1459         // If the value is already a scalar, then let the scalar change itself:
1460         m_value.GetScalar().SetValueFromCString (value_str, encoding, byte_size);
1461     }
1462     else if (byte_size <= Scalar::GetMaxByteSize())
1463     {
1464         // If the value fits in a scalar, then make a new scalar and again let the
1465         // scalar code do the conversion, then figure out where to put the new value.
1466         Scalar new_scalar;
1467         Error error;
1468         error = new_scalar.SetValueFromCString (value_str, encoding, byte_size);
1469         if (error.Success())
1470         {
1471             switch (value_type)
1472             {
1473                 case Value::eValueTypeLoadAddress:
1474                 {
1475                     // If it is a load address, then the scalar value is the storage location
1476                     // of the data, and we have to shove this value down to that load location.
1477                     ProcessSP process_sp = GetUpdatePoint().GetProcessSP();
1478                     if (process_sp)
1479                     {
1480                         addr_t target_addr = m_value.GetScalar().GetRawBits64(LLDB_INVALID_ADDRESS);
1481                         size_t bytes_written = process_sp->WriteScalarToMemory (target_addr,
1482                                                                             new_scalar,
1483                                                                             byte_size,
1484                                                                             error);
1485                         if (!error.Success() || bytes_written != byte_size)
1486                             return false;
1487                     }
1488                 }
1489                 break;
1490                 case Value::eValueTypeHostAddress:
1491                 {
1492                     // If it is a host address, then we stuff the scalar as a DataBuffer into the Value's data.
1493                     DataExtractor new_data;
1494                     new_data.SetByteOrder (m_data.GetByteOrder());
1495 
1496                     DataBufferSP buffer_sp (new DataBufferHeap(byte_size, 0));
1497                     m_data.SetData(buffer_sp, 0);
1498                     bool success = new_scalar.GetData(new_data);
1499                     if (success)
1500                     {
1501                         new_data.CopyByteOrderedData(0,
1502                                                      byte_size,
1503                                                      const_cast<uint8_t *>(m_data.GetDataStart()),
1504                                                      byte_size,
1505                                                      m_data.GetByteOrder());
1506                     }
1507                     m_value.GetScalar() = (uintptr_t)m_data.GetDataStart();
1508 
1509                 }
1510                 break;
1511                 case Value::eValueTypeFileAddress:
1512                 case Value::eValueTypeScalar:
1513                 break;
1514             }
1515         }
1516         else
1517         {
1518             return false;
1519         }
1520     }
1521     else
1522     {
1523         // We don't support setting things bigger than a scalar at present.
1524         return false;
1525     }
1526 
1527     // If we have reached this point, then we have successfully changed the value.
1528     SetNeedsUpdate();
1529     return true;
1530 }
1531 
1532 LanguageType
1533 ValueObject::GetObjectRuntimeLanguage ()
1534 {
1535     return ClangASTType::GetMinimumLanguage (GetClangAST(),
1536                                              GetClangType());
1537 }
1538 
1539 void
1540 ValueObject::AddSyntheticChild (const ConstString &key, ValueObject *valobj)
1541 {
1542     m_synthetic_children[key] = valobj;
1543 }
1544 
1545 ValueObjectSP
1546 ValueObject::GetSyntheticChild (const ConstString &key) const
1547 {
1548     ValueObjectSP synthetic_child_sp;
1549     std::map<ConstString, ValueObject *>::const_iterator pos = m_synthetic_children.find (key);
1550     if (pos != m_synthetic_children.end())
1551         synthetic_child_sp = pos->second->GetSP();
1552     return synthetic_child_sp;
1553 }
1554 
1555 bool
1556 ValueObject::IsPointerType ()
1557 {
1558     return ClangASTContext::IsPointerType (GetClangType());
1559 }
1560 
1561 bool
1562 ValueObject::IsArrayType ()
1563 {
1564     return ClangASTContext::IsArrayType (GetClangType());
1565 }
1566 
1567 bool
1568 ValueObject::IsScalarType ()
1569 {
1570     return ClangASTContext::IsScalarType (GetClangType());
1571 }
1572 
1573 bool
1574 ValueObject::IsIntegerType (bool &is_signed)
1575 {
1576     return ClangASTContext::IsIntegerType (GetClangType(), is_signed);
1577 }
1578 
1579 bool
1580 ValueObject::IsPointerOrReferenceType ()
1581 {
1582     return ClangASTContext::IsPointerOrReferenceType (GetClangType());
1583 }
1584 
1585 bool
1586 ValueObject::IsPossibleCPlusPlusDynamicType ()
1587 {
1588     return ClangASTContext::IsPossibleCPlusPlusDynamicType (GetClangAST (), GetClangType());
1589 }
1590 
1591 bool
1592 ValueObject::IsPossibleDynamicType ()
1593 {
1594     return ClangASTContext::IsPossibleDynamicType (GetClangAST (), GetClangType());
1595 }
1596 
1597 ValueObjectSP
1598 ValueObject::GetSyntheticArrayMember (int32_t index, bool can_create)
1599 {
1600     if (IsArrayType())
1601         return GetSyntheticArrayMemberFromArray(index, can_create);
1602 
1603     if (IsPointerType())
1604         return GetSyntheticArrayMemberFromPointer(index, can_create);
1605 
1606     return ValueObjectSP();
1607 
1608 }
1609 
1610 ValueObjectSP
1611 ValueObject::GetSyntheticArrayMemberFromPointer (int32_t index, bool can_create)
1612 {
1613     ValueObjectSP synthetic_child_sp;
1614     if (IsPointerType ())
1615     {
1616         char index_str[64];
1617         snprintf(index_str, sizeof(index_str), "[%i]", index);
1618         ConstString index_const_str(index_str);
1619         // Check if we have already created a synthetic array member in this
1620         // valid object. If we have we will re-use it.
1621         synthetic_child_sp = GetSyntheticChild (index_const_str);
1622         if (!synthetic_child_sp)
1623         {
1624             ValueObject *synthetic_child;
1625             // We haven't made a synthetic array member for INDEX yet, so
1626             // lets make one and cache it for any future reference.
1627             synthetic_child = CreateChildAtIndex(0, true, index);
1628 
1629             // Cache the value if we got one back...
1630             if (synthetic_child)
1631             {
1632                 AddSyntheticChild(index_const_str, synthetic_child);
1633                 synthetic_child_sp = synthetic_child->GetSP();
1634                 synthetic_child_sp->SetName(ConstString(index_str));
1635                 synthetic_child_sp->m_is_array_item_for_pointer = true;
1636             }
1637         }
1638     }
1639     return synthetic_child_sp;
1640 }
1641 
1642 // This allows you to create an array member using and index
1643 // that doesn't not fall in the normal bounds of the array.
1644 // Many times structure can be defined as:
1645 // struct Collection
1646 // {
1647 //     uint32_t item_count;
1648 //     Item item_array[0];
1649 // };
1650 // The size of the "item_array" is 1, but many times in practice
1651 // there are more items in "item_array".
1652 
1653 ValueObjectSP
1654 ValueObject::GetSyntheticArrayMemberFromArray (int32_t index, bool can_create)
1655 {
1656     ValueObjectSP synthetic_child_sp;
1657     if (IsArrayType ())
1658     {
1659         char index_str[64];
1660         snprintf(index_str, sizeof(index_str), "[%i]", index);
1661         ConstString index_const_str(index_str);
1662         // Check if we have already created a synthetic array member in this
1663         // valid object. If we have we will re-use it.
1664         synthetic_child_sp = GetSyntheticChild (index_const_str);
1665         if (!synthetic_child_sp)
1666         {
1667             ValueObject *synthetic_child;
1668             // We haven't made a synthetic array member for INDEX yet, so
1669             // lets make one and cache it for any future reference.
1670             synthetic_child = CreateChildAtIndex(0, true, index);
1671 
1672             // Cache the value if we got one back...
1673             if (synthetic_child)
1674             {
1675                 AddSyntheticChild(index_const_str, synthetic_child);
1676                 synthetic_child_sp = synthetic_child->GetSP();
1677                 synthetic_child_sp->SetName(ConstString(index_str));
1678                 synthetic_child_sp->m_is_array_item_for_pointer = true;
1679             }
1680         }
1681     }
1682     return synthetic_child_sp;
1683 }
1684 
1685 ValueObjectSP
1686 ValueObject::GetSyntheticBitFieldChild (uint32_t from, uint32_t to, bool can_create)
1687 {
1688     ValueObjectSP synthetic_child_sp;
1689     if (IsScalarType ())
1690     {
1691         char index_str[64];
1692         snprintf(index_str, sizeof(index_str), "[%i-%i]", from, to);
1693         ConstString index_const_str(index_str);
1694         // Check if we have already created a synthetic array member in this
1695         // valid object. If we have we will re-use it.
1696         synthetic_child_sp = GetSyntheticChild (index_const_str);
1697         if (!synthetic_child_sp)
1698         {
1699             ValueObjectChild *synthetic_child;
1700             // We haven't made a synthetic array member for INDEX yet, so
1701             // lets make one and cache it for any future reference.
1702             synthetic_child = new ValueObjectChild(*this,
1703                                                       GetClangAST(),
1704                                                       GetClangType(),
1705                                                       index_const_str,
1706                                                       GetByteSize(),
1707                                                       0,
1708                                                       to-from+1,
1709                                                       from,
1710                                                       false,
1711                                                       false,
1712                                                       eAddressTypeInvalid);
1713 
1714             // Cache the value if we got one back...
1715             if (synthetic_child)
1716             {
1717                 AddSyntheticChild(index_const_str, synthetic_child);
1718                 synthetic_child_sp = synthetic_child->GetSP();
1719                 synthetic_child_sp->SetName(ConstString(index_str));
1720                 synthetic_child_sp->m_is_bitfield_for_scalar = true;
1721             }
1722         }
1723     }
1724     return synthetic_child_sp;
1725 }
1726 
1727 ValueObjectSP
1728 ValueObject::GetSyntheticArrayRangeChild (uint32_t from, uint32_t to, bool can_create)
1729 {
1730     ValueObjectSP synthetic_child_sp;
1731     if (IsArrayType () || IsPointerType ())
1732     {
1733         char index_str[64];
1734         snprintf(index_str, sizeof(index_str), "[%i-%i]", from, to);
1735         ConstString index_const_str(index_str);
1736         // Check if we have already created a synthetic array member in this
1737         // valid object. If we have we will re-use it.
1738         synthetic_child_sp = GetSyntheticChild (index_const_str);
1739         if (!synthetic_child_sp)
1740         {
1741             ValueObjectSynthetic *synthetic_child;
1742 
1743             // We haven't made a synthetic array member for INDEX yet, so
1744             // lets make one and cache it for any future reference.
1745             SyntheticArrayView *view = new SyntheticArrayView();
1746             view->AddRange(from,to);
1747             SyntheticChildrenSP view_sp(view);
1748             synthetic_child = new ValueObjectSynthetic(*this, view_sp);
1749 
1750             // Cache the value if we got one back...
1751             if (synthetic_child)
1752             {
1753                 AddSyntheticChild(index_const_str, synthetic_child);
1754                 synthetic_child_sp = synthetic_child->GetSP();
1755                 synthetic_child_sp->SetName(ConstString(index_str));
1756                 synthetic_child_sp->m_is_bitfield_for_scalar = true;
1757             }
1758         }
1759     }
1760     return synthetic_child_sp;
1761 }
1762 
1763 ValueObjectSP
1764 ValueObject::GetSyntheticChildAtOffset(uint32_t offset, const ClangASTType& type, bool can_create)
1765 {
1766 
1767     ValueObjectSP synthetic_child_sp;
1768 
1769     char name_str[64];
1770     snprintf(name_str, sizeof(name_str), "@%i", offset);
1771     ConstString name_const_str(name_str);
1772 
1773     // Check if we have already created a synthetic array member in this
1774     // valid object. If we have we will re-use it.
1775     synthetic_child_sp = GetSyntheticChild (name_const_str);
1776 
1777     if (synthetic_child_sp.get())
1778         return synthetic_child_sp;
1779 
1780     if (!can_create)
1781         return ValueObjectSP();
1782 
1783     ValueObjectChild *synthetic_child = new ValueObjectChild(*this,
1784                                                              type.GetASTContext(),
1785                                                              type.GetOpaqueQualType(),
1786                                                              name_const_str,
1787                                                              type.GetTypeByteSize(),
1788                                                              offset,
1789                                                              0,
1790                                                              0,
1791                                                              false,
1792                                                              false,
1793                                                              eAddressTypeInvalid);
1794     if (synthetic_child)
1795     {
1796         AddSyntheticChild(name_const_str, synthetic_child);
1797         synthetic_child_sp = synthetic_child->GetSP();
1798         synthetic_child_sp->SetName(name_const_str);
1799         synthetic_child_sp->m_is_child_at_offset = true;
1800     }
1801     return synthetic_child_sp;
1802 }
1803 
1804 // your expression path needs to have a leading . or ->
1805 // (unless it somehow "looks like" an array, in which case it has
1806 // a leading [ symbol). while the [ is meaningful and should be shown
1807 // to the user, . and -> are just parser design, but by no means
1808 // added information for the user.. strip them off
1809 static const char*
1810 SkipLeadingExpressionPathSeparators(const char* expression)
1811 {
1812     if (!expression || !expression[0])
1813         return expression;
1814     if (expression[0] == '.')
1815         return expression+1;
1816     if (expression[0] == '-' && expression[1] == '>')
1817         return expression+2;
1818     return expression;
1819 }
1820 
1821 ValueObjectSP
1822 ValueObject::GetSyntheticExpressionPathChild(const char* expression, bool can_create)
1823 {
1824     ValueObjectSP synthetic_child_sp;
1825     ConstString name_const_string(expression);
1826     // Check if we have already created a synthetic array member in this
1827     // valid object. If we have we will re-use it.
1828     synthetic_child_sp = GetSyntheticChild (name_const_string);
1829     if (!synthetic_child_sp)
1830     {
1831         // We haven't made a synthetic array member for expression yet, so
1832         // lets make one and cache it for any future reference.
1833         synthetic_child_sp = GetValueForExpressionPath(expression);
1834 
1835         // Cache the value if we got one back...
1836         if (synthetic_child_sp.get())
1837         {
1838             AddSyntheticChild(name_const_string, synthetic_child_sp.get());
1839             synthetic_child_sp->SetName(ConstString(SkipLeadingExpressionPathSeparators(expression)));
1840             synthetic_child_sp->m_is_expression_path_child = true;
1841         }
1842     }
1843     return synthetic_child_sp;
1844 }
1845 
1846 void
1847 ValueObject::CalculateSyntheticValue (SyntheticValueType use_synthetic)
1848 {
1849     if (use_synthetic == eNoSyntheticFilter)
1850         return;
1851 
1852     UpdateFormatsIfNeeded(m_last_format_mgr_dynamic);
1853 
1854     if (m_last_synthetic_filter.get() == NULL)
1855         return;
1856 
1857     if (m_synthetic_value == NULL)
1858         m_synthetic_value = new ValueObjectSynthetic(*this, m_last_synthetic_filter);
1859 
1860 }
1861 
1862 void
1863 ValueObject::CalculateDynamicValue (DynamicValueType use_dynamic)
1864 {
1865     if (use_dynamic == eNoDynamicValues)
1866         return;
1867 
1868     if (!m_dynamic_value && !IsDynamic())
1869     {
1870         Process *process = m_update_point.GetProcessSP().get();
1871         bool worth_having_dynamic_value = false;
1872 
1873 
1874         // FIXME: Process should have some kind of "map over Runtimes" so we don't have to
1875         // hard code this everywhere.
1876         LanguageType known_type = GetObjectRuntimeLanguage();
1877         if (known_type != eLanguageTypeUnknown && known_type != eLanguageTypeC)
1878         {
1879             LanguageRuntime *runtime = process->GetLanguageRuntime (known_type);
1880             if (runtime)
1881                 worth_having_dynamic_value = runtime->CouldHaveDynamicValue(*this);
1882         }
1883         else
1884         {
1885             LanguageRuntime *cpp_runtime = process->GetLanguageRuntime (eLanguageTypeC_plus_plus);
1886             if (cpp_runtime)
1887                 worth_having_dynamic_value = cpp_runtime->CouldHaveDynamicValue(*this);
1888 
1889             if (!worth_having_dynamic_value)
1890             {
1891                 LanguageRuntime *objc_runtime = process->GetLanguageRuntime (eLanguageTypeObjC);
1892                 if (objc_runtime)
1893                     worth_having_dynamic_value = objc_runtime->CouldHaveDynamicValue(*this);
1894             }
1895         }
1896 
1897         if (worth_having_dynamic_value)
1898             m_dynamic_value = new ValueObjectDynamicValue (*this, use_dynamic);
1899 
1900 //        if (worth_having_dynamic_value)
1901 //            printf ("Adding dynamic value %s (%p) to (%p) - manager %p.\n", m_name.GetCString(), m_dynamic_value, this, m_manager);
1902 
1903     }
1904 }
1905 
1906 ValueObjectSP
1907 ValueObject::GetDynamicValue (DynamicValueType use_dynamic)
1908 {
1909     if (use_dynamic == eNoDynamicValues)
1910         return ValueObjectSP();
1911 
1912     if (!IsDynamic() && m_dynamic_value == NULL)
1913     {
1914         CalculateDynamicValue(use_dynamic);
1915     }
1916     if (m_dynamic_value)
1917         return m_dynamic_value->GetSP();
1918     else
1919         return ValueObjectSP();
1920 }
1921 
1922 // GetDynamicValue() returns a NULL SharedPointer if the object is not dynamic
1923 // or we do not really want a dynamic VO. this method instead returns this object
1924 // itself when making it synthetic has no meaning. this makes it much simpler
1925 // to replace the SyntheticValue for the ValueObject
1926 ValueObjectSP
1927 ValueObject::GetSyntheticValue (SyntheticValueType use_synthetic)
1928 {
1929     if (use_synthetic == eNoSyntheticFilter)
1930         return GetSP();
1931 
1932     UpdateFormatsIfNeeded(m_last_format_mgr_dynamic);
1933 
1934     if (m_last_synthetic_filter.get() == NULL)
1935         return GetSP();
1936 
1937     CalculateSyntheticValue(use_synthetic);
1938 
1939     if (m_synthetic_value)
1940         return m_synthetic_value->GetSP();
1941     else
1942         return GetSP();
1943 }
1944 
1945 bool
1946 ValueObject::HasSyntheticValue()
1947 {
1948     UpdateFormatsIfNeeded(m_last_format_mgr_dynamic);
1949 
1950     if (m_last_synthetic_filter.get() == NULL)
1951         return false;
1952 
1953     CalculateSyntheticValue(eUseSyntheticFilter);
1954 
1955     if (m_synthetic_value)
1956         return true;
1957     else
1958         return false;
1959 }
1960 
1961 bool
1962 ValueObject::GetBaseClassPath (Stream &s)
1963 {
1964     if (IsBaseClass())
1965     {
1966         bool parent_had_base_class = GetParent() && GetParent()->GetBaseClassPath (s);
1967         clang_type_t clang_type = GetClangType();
1968         std::string cxx_class_name;
1969         bool this_had_base_class = ClangASTContext::GetCXXClassName (clang_type, cxx_class_name);
1970         if (this_had_base_class)
1971         {
1972             if (parent_had_base_class)
1973                 s.PutCString("::");
1974             s.PutCString(cxx_class_name.c_str());
1975         }
1976         return parent_had_base_class || this_had_base_class;
1977     }
1978     return false;
1979 }
1980 
1981 
1982 ValueObject *
1983 ValueObject::GetNonBaseClassParent()
1984 {
1985     if (GetParent())
1986     {
1987         if (GetParent()->IsBaseClass())
1988             return GetParent()->GetNonBaseClassParent();
1989         else
1990             return GetParent();
1991     }
1992     return NULL;
1993 }
1994 
1995 void
1996 ValueObject::GetExpressionPath (Stream &s, bool qualify_cxx_base_classes, GetExpressionPathFormat epformat)
1997 {
1998     const bool is_deref_of_parent = IsDereferenceOfParent ();
1999 
2000     if (is_deref_of_parent && epformat == eDereferencePointers)
2001     {
2002         // this is the original format of GetExpressionPath() producing code like *(a_ptr).memberName, which is entirely
2003         // fine, until you put this into StackFrame::GetValueForVariableExpressionPath() which prefers to see a_ptr->memberName.
2004         // the eHonorPointers mode is meant to produce strings in this latter format
2005         s.PutCString("*(");
2006     }
2007 
2008     ValueObject* parent = GetParent();
2009 
2010     if (parent)
2011         parent->GetExpressionPath (s, qualify_cxx_base_classes, epformat);
2012 
2013     // if we are a deref_of_parent just because we are synthetic array
2014     // members made up to allow ptr[%d] syntax to work in variable
2015     // printing, then add our name ([%d]) to the expression path
2016     if (m_is_array_item_for_pointer && epformat == eHonorPointers)
2017         s.PutCString(m_name.AsCString());
2018 
2019     if (!IsBaseClass())
2020     {
2021         if (!is_deref_of_parent)
2022         {
2023             ValueObject *non_base_class_parent = GetNonBaseClassParent();
2024             if (non_base_class_parent)
2025             {
2026                 clang_type_t non_base_class_parent_clang_type = non_base_class_parent->GetClangType();
2027                 if (non_base_class_parent_clang_type)
2028                 {
2029                     const uint32_t non_base_class_parent_type_info = ClangASTContext::GetTypeInfo (non_base_class_parent_clang_type, NULL, NULL);
2030 
2031                     if (parent && parent->IsDereferenceOfParent() && epformat == eHonorPointers)
2032                     {
2033                         s.PutCString("->");
2034                     }
2035                     else
2036                     {
2037                         if (non_base_class_parent_type_info & ClangASTContext::eTypeIsPointer)
2038                         {
2039                             s.PutCString("->");
2040                         }
2041                         else if ((non_base_class_parent_type_info & ClangASTContext::eTypeHasChildren) &&
2042                                  !(non_base_class_parent_type_info & ClangASTContext::eTypeIsArray))
2043                         {
2044                             s.PutChar('.');
2045                         }
2046                     }
2047                 }
2048             }
2049 
2050             const char *name = GetName().GetCString();
2051             if (name)
2052             {
2053                 if (qualify_cxx_base_classes)
2054                 {
2055                     if (GetBaseClassPath (s))
2056                         s.PutCString("::");
2057                 }
2058                 s.PutCString(name);
2059             }
2060         }
2061     }
2062 
2063     if (is_deref_of_parent && epformat == eDereferencePointers)
2064     {
2065         s.PutChar(')');
2066     }
2067 }
2068 
2069 ValueObjectSP
2070 ValueObject::GetValueForExpressionPath(const char* expression,
2071                                        const char** first_unparsed,
2072                                        ExpressionPathScanEndReason* reason_to_stop,
2073                                        ExpressionPathEndResultType* final_value_type,
2074                                        const GetValueForExpressionPathOptions& options,
2075                                        ExpressionPathAftermath* final_task_on_target)
2076 {
2077 
2078     const char* dummy_first_unparsed;
2079     ExpressionPathScanEndReason dummy_reason_to_stop;
2080     ExpressionPathEndResultType dummy_final_value_type;
2081     ExpressionPathAftermath dummy_final_task_on_target = ValueObject::eNothing;
2082 
2083     ValueObjectSP ret_val = GetValueForExpressionPath_Impl(expression,
2084                                                            first_unparsed ? first_unparsed : &dummy_first_unparsed,
2085                                                            reason_to_stop ? reason_to_stop : &dummy_reason_to_stop,
2086                                                            final_value_type ? final_value_type : &dummy_final_value_type,
2087                                                            options,
2088                                                            final_task_on_target ? final_task_on_target : &dummy_final_task_on_target);
2089 
2090     if (!final_task_on_target || *final_task_on_target == ValueObject::eNothing)
2091     {
2092         return ret_val;
2093     }
2094     if (ret_val.get() && *final_value_type == ePlain) // I can only deref and takeaddress of plain objects
2095     {
2096         if (*final_task_on_target == ValueObject::eDereference)
2097         {
2098             Error error;
2099             ValueObjectSP final_value = ret_val->Dereference(error);
2100             if (error.Fail() || !final_value.get())
2101             {
2102                 *reason_to_stop = ValueObject::eDereferencingFailed;
2103                 *final_value_type = ValueObject::eInvalid;
2104                 return ValueObjectSP();
2105             }
2106             else
2107             {
2108                 *final_task_on_target = ValueObject::eNothing;
2109                 return final_value;
2110             }
2111         }
2112         if (*final_task_on_target == ValueObject::eTakeAddress)
2113         {
2114             Error error;
2115             ValueObjectSP final_value = ret_val->AddressOf(error);
2116             if (error.Fail() || !final_value.get())
2117             {
2118                 *reason_to_stop = ValueObject::eTakingAddressFailed;
2119                 *final_value_type = ValueObject::eInvalid;
2120                 return ValueObjectSP();
2121             }
2122             else
2123             {
2124                 *final_task_on_target = ValueObject::eNothing;
2125                 return final_value;
2126             }
2127         }
2128     }
2129     return ret_val; // final_task_on_target will still have its original value, so you know I did not do it
2130 }
2131 
2132 int
2133 ValueObject::GetValuesForExpressionPath(const char* expression,
2134                                         ValueObjectListSP& list,
2135                                         const char** first_unparsed,
2136                                         ExpressionPathScanEndReason* reason_to_stop,
2137                                         ExpressionPathEndResultType* final_value_type,
2138                                         const GetValueForExpressionPathOptions& options,
2139                                         ExpressionPathAftermath* final_task_on_target)
2140 {
2141     const char* dummy_first_unparsed;
2142     ExpressionPathScanEndReason dummy_reason_to_stop;
2143     ExpressionPathEndResultType dummy_final_value_type;
2144     ExpressionPathAftermath dummy_final_task_on_target = ValueObject::eNothing;
2145 
2146     ValueObjectSP ret_val = GetValueForExpressionPath_Impl(expression,
2147                                                            first_unparsed ? first_unparsed : &dummy_first_unparsed,
2148                                                            reason_to_stop ? reason_to_stop : &dummy_reason_to_stop,
2149                                                            final_value_type ? final_value_type : &dummy_final_value_type,
2150                                                            options,
2151                                                            final_task_on_target ? final_task_on_target : &dummy_final_task_on_target);
2152 
2153     if (!ret_val.get()) // if there are errors, I add nothing to the list
2154         return 0;
2155 
2156     if (*reason_to_stop != eArrayRangeOperatorMet)
2157     {
2158         // I need not expand a range, just post-process the final value and return
2159         if (!final_task_on_target || *final_task_on_target == ValueObject::eNothing)
2160         {
2161             list->Append(ret_val);
2162             return 1;
2163         }
2164         if (ret_val.get() && *final_value_type == ePlain) // I can only deref and takeaddress of plain objects
2165         {
2166             if (*final_task_on_target == ValueObject::eDereference)
2167             {
2168                 Error error;
2169                 ValueObjectSP final_value = ret_val->Dereference(error);
2170                 if (error.Fail() || !final_value.get())
2171                 {
2172                     *reason_to_stop = ValueObject::eDereferencingFailed;
2173                     *final_value_type = ValueObject::eInvalid;
2174                     return 0;
2175                 }
2176                 else
2177                 {
2178                     *final_task_on_target = ValueObject::eNothing;
2179                     list->Append(final_value);
2180                     return 1;
2181                 }
2182             }
2183             if (*final_task_on_target == ValueObject::eTakeAddress)
2184             {
2185                 Error error;
2186                 ValueObjectSP final_value = ret_val->AddressOf(error);
2187                 if (error.Fail() || !final_value.get())
2188                 {
2189                     *reason_to_stop = ValueObject::eTakingAddressFailed;
2190                     *final_value_type = ValueObject::eInvalid;
2191                     return 0;
2192                 }
2193                 else
2194                 {
2195                     *final_task_on_target = ValueObject::eNothing;
2196                     list->Append(final_value);
2197                     return 1;
2198                 }
2199             }
2200         }
2201     }
2202     else
2203     {
2204         return ExpandArraySliceExpression(first_unparsed ? *first_unparsed : dummy_first_unparsed,
2205                                           first_unparsed ? first_unparsed : &dummy_first_unparsed,
2206                                           ret_val,
2207                                           list,
2208                                           reason_to_stop ? reason_to_stop : &dummy_reason_to_stop,
2209                                           final_value_type ? final_value_type : &dummy_final_value_type,
2210                                           options,
2211                                           final_task_on_target ? final_task_on_target : &dummy_final_task_on_target);
2212     }
2213     // in any non-covered case, just do the obviously right thing
2214     list->Append(ret_val);
2215     return 1;
2216 }
2217 
2218 ValueObjectSP
2219 ValueObject::GetValueForExpressionPath_Impl(const char* expression_cstr,
2220                                             const char** first_unparsed,
2221                                             ExpressionPathScanEndReason* reason_to_stop,
2222                                             ExpressionPathEndResultType* final_result,
2223                                             const GetValueForExpressionPathOptions& options,
2224                                             ExpressionPathAftermath* what_next)
2225 {
2226     ValueObjectSP root = GetSP();
2227 
2228     if (!root.get())
2229         return ValueObjectSP();
2230 
2231     *first_unparsed = expression_cstr;
2232 
2233     while (true)
2234     {
2235 
2236         const char* expression_cstr = *first_unparsed; // hide the top level expression_cstr
2237 
2238         clang_type_t root_clang_type = root->GetClangType();
2239         clang_type_t pointee_clang_type;
2240         Flags root_clang_type_info,pointee_clang_type_info;
2241 
2242         root_clang_type_info = Flags(ClangASTContext::GetTypeInfo(root_clang_type, GetClangAST(), &pointee_clang_type));
2243         if (pointee_clang_type)
2244             pointee_clang_type_info = Flags(ClangASTContext::GetTypeInfo(pointee_clang_type, GetClangAST(), NULL));
2245 
2246         if (!expression_cstr || *expression_cstr == '\0')
2247         {
2248             *reason_to_stop = ValueObject::eEndOfString;
2249             return root;
2250         }
2251 
2252         switch (*expression_cstr)
2253         {
2254             case '-':
2255             {
2256                 if (options.m_check_dot_vs_arrow_syntax &&
2257                     root_clang_type_info.Test(ClangASTContext::eTypeIsPointer) ) // if you are trying to use -> on a non-pointer and I must catch the error
2258                 {
2259                     *first_unparsed = expression_cstr;
2260                     *reason_to_stop = ValueObject::eArrowInsteadOfDot;
2261                     *final_result = ValueObject::eInvalid;
2262                     return ValueObjectSP();
2263                 }
2264                 if (root_clang_type_info.Test(ClangASTContext::eTypeIsObjC) &&  // if yo are trying to extract an ObjC IVar when this is forbidden
2265                     root_clang_type_info.Test(ClangASTContext::eTypeIsPointer) &&
2266                     options.m_no_fragile_ivar)
2267                 {
2268                     *first_unparsed = expression_cstr;
2269                     *reason_to_stop = ValueObject::eFragileIVarNotAllowed;
2270                     *final_result = ValueObject::eInvalid;
2271                     return ValueObjectSP();
2272                 }
2273                 if (expression_cstr[1] != '>')
2274                 {
2275                     *first_unparsed = expression_cstr;
2276                     *reason_to_stop = ValueObject::eUnexpectedSymbol;
2277                     *final_result = ValueObject::eInvalid;
2278                     return ValueObjectSP();
2279                 }
2280                 expression_cstr++; // skip the -
2281             }
2282             case '.': // or fallthrough from ->
2283             {
2284                 if (options.m_check_dot_vs_arrow_syntax && *expression_cstr == '.' &&
2285                     root_clang_type_info.Test(ClangASTContext::eTypeIsPointer)) // if you are trying to use . on a pointer and I must catch the error
2286                 {
2287                     *first_unparsed = expression_cstr;
2288                     *reason_to_stop = ValueObject::eDotInsteadOfArrow;
2289                     *final_result = ValueObject::eInvalid;
2290                     return ValueObjectSP();
2291                 }
2292                 expression_cstr++; // skip .
2293                 const char *next_separator = strpbrk(expression_cstr+1,"-.[");
2294                 ConstString child_name;
2295                 if (!next_separator) // if no other separator just expand this last layer
2296                 {
2297                     child_name.SetCString (expression_cstr);
2298                     ValueObjectSP child_valobj_sp = root->GetChildMemberWithName(child_name, true);
2299 
2300                     if (child_valobj_sp.get()) // we know we are done, so just return
2301                     {
2302                         *first_unparsed = '\0';
2303                         *reason_to_stop = ValueObject::eEndOfString;
2304                         *final_result = ValueObject::ePlain;
2305                         return child_valobj_sp;
2306                     }
2307                     else if (options.m_no_synthetic_children == false) // let's try with synthetic children
2308                     {
2309                         child_valobj_sp = root->GetSyntheticValue(eUseSyntheticFilter)->GetChildMemberWithName(child_name, true);
2310                     }
2311 
2312                     // if we are here and options.m_no_synthetic_children is true, child_valobj_sp is going to be a NULL SP,
2313                     // so we hit the "else" branch, and return an error
2314                     if(child_valobj_sp.get()) // if it worked, just return
2315                     {
2316                         *first_unparsed = '\0';
2317                         *reason_to_stop = ValueObject::eEndOfString;
2318                         *final_result = ValueObject::ePlain;
2319                         return child_valobj_sp;
2320                     }
2321                     else
2322                     {
2323                         *first_unparsed = expression_cstr;
2324                         *reason_to_stop = ValueObject::eNoSuchChild;
2325                         *final_result = ValueObject::eInvalid;
2326                         return ValueObjectSP();
2327                     }
2328                 }
2329                 else // other layers do expand
2330                 {
2331                     child_name.SetCStringWithLength(expression_cstr, next_separator - expression_cstr);
2332                     ValueObjectSP child_valobj_sp = root->GetChildMemberWithName(child_name, true);
2333                     if (child_valobj_sp.get()) // store the new root and move on
2334                     {
2335                         root = child_valobj_sp;
2336                         *first_unparsed = next_separator;
2337                         *final_result = ValueObject::ePlain;
2338                         continue;
2339                     }
2340                     else if (options.m_no_synthetic_children == false) // let's try with synthetic children
2341                     {
2342                         child_valobj_sp = root->GetSyntheticValue(eUseSyntheticFilter)->GetChildMemberWithName(child_name, true);
2343                     }
2344 
2345                     // if we are here and options.m_no_synthetic_children is true, child_valobj_sp is going to be a NULL SP,
2346                     // so we hit the "else" branch, and return an error
2347                     if(child_valobj_sp.get()) // if it worked, move on
2348                     {
2349                         root = child_valobj_sp;
2350                         *first_unparsed = next_separator;
2351                         *final_result = ValueObject::ePlain;
2352                         continue;
2353                     }
2354                     else
2355                     {
2356                         *first_unparsed = expression_cstr;
2357                         *reason_to_stop = ValueObject::eNoSuchChild;
2358                         *final_result = ValueObject::eInvalid;
2359                         return ValueObjectSP();
2360                     }
2361                 }
2362                 break;
2363             }
2364             case '[':
2365             {
2366                 if (!root_clang_type_info.Test(ClangASTContext::eTypeIsArray) && !root_clang_type_info.Test(ClangASTContext::eTypeIsPointer)) // if this is not a T[] nor a T*
2367                 {
2368                     if (!root_clang_type_info.Test(ClangASTContext::eTypeIsScalar)) // if this is not even a scalar...
2369                     {
2370                         if (options.m_no_synthetic_children) // ...only chance left is synthetic
2371                         {
2372                             *first_unparsed = expression_cstr;
2373                             *reason_to_stop = ValueObject::eRangeOperatorInvalid;
2374                             *final_result = ValueObject::eInvalid;
2375                             return ValueObjectSP();
2376                         }
2377                     }
2378                     else if (!options.m_allow_bitfields_syntax) // if this is a scalar, check that we can expand bitfields
2379                     {
2380                         *first_unparsed = expression_cstr;
2381                         *reason_to_stop = ValueObject::eRangeOperatorNotAllowed;
2382                         *final_result = ValueObject::eInvalid;
2383                         return ValueObjectSP();
2384                     }
2385                 }
2386                 if (*(expression_cstr+1) == ']') // if this is an unbounded range it only works for arrays
2387                 {
2388                     if (!root_clang_type_info.Test(ClangASTContext::eTypeIsArray))
2389                     {
2390                         *first_unparsed = expression_cstr;
2391                         *reason_to_stop = ValueObject::eEmptyRangeNotAllowed;
2392                         *final_result = ValueObject::eInvalid;
2393                         return ValueObjectSP();
2394                     }
2395                     else // even if something follows, we cannot expand unbounded ranges, just let the caller do it
2396                     {
2397                         *first_unparsed = expression_cstr+2;
2398                         *reason_to_stop = ValueObject::eArrayRangeOperatorMet;
2399                         *final_result = ValueObject::eUnboundedRange;
2400                         return root;
2401                     }
2402                 }
2403                 const char *separator_position = ::strchr(expression_cstr+1,'-');
2404                 const char *close_bracket_position = ::strchr(expression_cstr+1,']');
2405                 if (!close_bracket_position) // if there is no ], this is a syntax error
2406                 {
2407                     *first_unparsed = expression_cstr;
2408                     *reason_to_stop = ValueObject::eUnexpectedSymbol;
2409                     *final_result = ValueObject::eInvalid;
2410                     return ValueObjectSP();
2411                 }
2412                 if (!separator_position || separator_position > close_bracket_position) // if no separator, this is either [] or [N]
2413                 {
2414                     char *end = NULL;
2415                     unsigned long index = ::strtoul (expression_cstr+1, &end, 0);
2416                     if (!end || end != close_bracket_position) // if something weird is in our way return an error
2417                     {
2418                         *first_unparsed = expression_cstr;
2419                         *reason_to_stop = ValueObject::eUnexpectedSymbol;
2420                         *final_result = ValueObject::eInvalid;
2421                         return ValueObjectSP();
2422                     }
2423                     if (end - expression_cstr == 1) // if this is [], only return a valid value for arrays
2424                     {
2425                         if (root_clang_type_info.Test(ClangASTContext::eTypeIsArray))
2426                         {
2427                             *first_unparsed = expression_cstr+2;
2428                             *reason_to_stop = ValueObject::eArrayRangeOperatorMet;
2429                             *final_result = ValueObject::eUnboundedRange;
2430                             return root;
2431                         }
2432                         else
2433                         {
2434                             *first_unparsed = expression_cstr;
2435                             *reason_to_stop = ValueObject::eEmptyRangeNotAllowed;
2436                             *final_result = ValueObject::eInvalid;
2437                             return ValueObjectSP();
2438                         }
2439                     }
2440                     // from here on we do have a valid index
2441                     if (root_clang_type_info.Test(ClangASTContext::eTypeIsArray))
2442                     {
2443                         ValueObjectSP child_valobj_sp = root->GetChildAtIndex(index, true);
2444                         if (!child_valobj_sp)
2445                             child_valobj_sp = root->GetSyntheticArrayMemberFromArray(index, true);
2446                         if (!child_valobj_sp)
2447                             if (root->HasSyntheticValue() && root->GetSyntheticValue(eUseSyntheticFilter)->GetNumChildren() > index)
2448                                 child_valobj_sp = root->GetSyntheticValue(eUseSyntheticFilter)->GetChildAtIndex(index, true);
2449                         if (child_valobj_sp)
2450                         {
2451                             root = child_valobj_sp;
2452                             *first_unparsed = end+1; // skip ]
2453                             *final_result = ValueObject::ePlain;
2454                             continue;
2455                         }
2456                         else
2457                         {
2458                             *first_unparsed = expression_cstr;
2459                             *reason_to_stop = ValueObject::eNoSuchChild;
2460                             *final_result = ValueObject::eInvalid;
2461                             return ValueObjectSP();
2462                         }
2463                     }
2464                     else if (root_clang_type_info.Test(ClangASTContext::eTypeIsPointer))
2465                     {
2466                         if (*what_next == ValueObject::eDereference &&  // if this is a ptr-to-scalar, I am accessing it by index and I would have deref'ed anyway, then do it now and use this as a bitfield
2467                             pointee_clang_type_info.Test(ClangASTContext::eTypeIsScalar))
2468                         {
2469                             Error error;
2470                             root = root->Dereference(error);
2471                             if (error.Fail() || !root.get())
2472                             {
2473                                 *first_unparsed = expression_cstr;
2474                                 *reason_to_stop = ValueObject::eDereferencingFailed;
2475                                 *final_result = ValueObject::eInvalid;
2476                                 return ValueObjectSP();
2477                             }
2478                             else
2479                             {
2480                                 *what_next = eNothing;
2481                                 continue;
2482                             }
2483                         }
2484                         else
2485                         {
2486                             if (ClangASTType::GetMinimumLanguage(root->GetClangAST(),
2487                                                                     root->GetClangType()) == eLanguageTypeObjC
2488                                 &&
2489                                 ClangASTContext::IsPointerType(ClangASTType::GetPointeeType(root->GetClangType())) == false
2490                                 &&
2491                                 root->HasSyntheticValue()
2492                                 &&
2493                                 options.m_no_synthetic_children == false)
2494                             {
2495                                 root = root->GetSyntheticValue(eUseSyntheticFilter)->GetChildAtIndex(index, true);
2496                             }
2497                             else
2498                                 root = root->GetSyntheticArrayMemberFromPointer(index, true);
2499                             if (!root.get())
2500                             {
2501                                 *first_unparsed = expression_cstr;
2502                                 *reason_to_stop = ValueObject::eNoSuchChild;
2503                                 *final_result = ValueObject::eInvalid;
2504                                 return ValueObjectSP();
2505                             }
2506                             else
2507                             {
2508                                 *first_unparsed = end+1; // skip ]
2509                                 *final_result = ValueObject::ePlain;
2510                                 continue;
2511                             }
2512                         }
2513                     }
2514                     else if (ClangASTContext::IsScalarType(root_clang_type))
2515                     {
2516                         root = root->GetSyntheticBitFieldChild(index, index, true);
2517                         if (!root.get())
2518                         {
2519                             *first_unparsed = expression_cstr;
2520                             *reason_to_stop = ValueObject::eNoSuchChild;
2521                             *final_result = ValueObject::eInvalid;
2522                             return ValueObjectSP();
2523                         }
2524                         else // we do not know how to expand members of bitfields, so we just return and let the caller do any further processing
2525                         {
2526                             *first_unparsed = end+1; // skip ]
2527                             *reason_to_stop = ValueObject::eBitfieldRangeOperatorMet;
2528                             *final_result = ValueObject::eBitfield;
2529                             return root;
2530                         }
2531                     }
2532                     else if (root->HasSyntheticValue() && options.m_no_synthetic_children == false)
2533                     {
2534                         root = root->GetSyntheticValue(eUseSyntheticFilter)->GetChildAtIndex(index, true);
2535                         if (!root.get())
2536                         {
2537                             *first_unparsed = expression_cstr;
2538                             *reason_to_stop = ValueObject::eNoSuchChild;
2539                             *final_result = ValueObject::eInvalid;
2540                             return ValueObjectSP();
2541                         }
2542                         else
2543                         {
2544                             *first_unparsed = end+1; // skip ]
2545                             *final_result = ValueObject::ePlain;
2546                             continue;
2547                         }
2548                     }
2549                     else
2550                     {
2551                         *first_unparsed = expression_cstr;
2552                         *reason_to_stop = ValueObject::eNoSuchChild;
2553                         *final_result = ValueObject::eInvalid;
2554                         return ValueObjectSP();
2555                     }
2556                 }
2557                 else // we have a low and a high index
2558                 {
2559                     char *end = NULL;
2560                     unsigned long index_lower = ::strtoul (expression_cstr+1, &end, 0);
2561                     if (!end || end != separator_position) // if something weird is in our way return an error
2562                     {
2563                         *first_unparsed = expression_cstr;
2564                         *reason_to_stop = ValueObject::eUnexpectedSymbol;
2565                         *final_result = ValueObject::eInvalid;
2566                         return ValueObjectSP();
2567                     }
2568                     unsigned long index_higher = ::strtoul (separator_position+1, &end, 0);
2569                     if (!end || end != close_bracket_position) // if something weird is in our way return an error
2570                     {
2571                         *first_unparsed = expression_cstr;
2572                         *reason_to_stop = ValueObject::eUnexpectedSymbol;
2573                         *final_result = ValueObject::eInvalid;
2574                         return ValueObjectSP();
2575                     }
2576                     if (index_lower > index_higher) // swap indices if required
2577                     {
2578                         unsigned long temp = index_lower;
2579                         index_lower = index_higher;
2580                         index_higher = temp;
2581                     }
2582                     if (root_clang_type_info.Test(ClangASTContext::eTypeIsScalar)) // expansion only works for scalars
2583                     {
2584                         root = root->GetSyntheticBitFieldChild(index_lower, index_higher, true);
2585                         if (!root.get())
2586                         {
2587                             *first_unparsed = expression_cstr;
2588                             *reason_to_stop = ValueObject::eNoSuchChild;
2589                             *final_result = ValueObject::eInvalid;
2590                             return ValueObjectSP();
2591                         }
2592                         else
2593                         {
2594                             *first_unparsed = end+1; // skip ]
2595                             *reason_to_stop = ValueObject::eBitfieldRangeOperatorMet;
2596                             *final_result = ValueObject::eBitfield;
2597                             return root;
2598                         }
2599                     }
2600                     else if (root_clang_type_info.Test(ClangASTContext::eTypeIsPointer) && // if this is a ptr-to-scalar, I am accessing it by index and I would have deref'ed anyway, then do it now and use this as a bitfield
2601                              *what_next == ValueObject::eDereference &&
2602                              pointee_clang_type_info.Test(ClangASTContext::eTypeIsScalar))
2603                     {
2604                         Error error;
2605                         root = root->Dereference(error);
2606                         if (error.Fail() || !root.get())
2607                         {
2608                             *first_unparsed = expression_cstr;
2609                             *reason_to_stop = ValueObject::eDereferencingFailed;
2610                             *final_result = ValueObject::eInvalid;
2611                             return ValueObjectSP();
2612                         }
2613                         else
2614                         {
2615                             *what_next = ValueObject::eNothing;
2616                             continue;
2617                         }
2618                     }
2619                     else
2620                     {
2621                         *first_unparsed = expression_cstr;
2622                         *reason_to_stop = ValueObject::eArrayRangeOperatorMet;
2623                         *final_result = ValueObject::eBoundedRange;
2624                         return root;
2625                     }
2626                 }
2627                 break;
2628             }
2629             default: // some non-separator is in the way
2630             {
2631                 *first_unparsed = expression_cstr;
2632                 *reason_to_stop = ValueObject::eUnexpectedSymbol;
2633                 *final_result = ValueObject::eInvalid;
2634                 return ValueObjectSP();
2635                 break;
2636             }
2637         }
2638     }
2639 }
2640 
2641 int
2642 ValueObject::ExpandArraySliceExpression(const char* expression_cstr,
2643                                         const char** first_unparsed,
2644                                         ValueObjectSP root,
2645                                         ValueObjectListSP& list,
2646                                         ExpressionPathScanEndReason* reason_to_stop,
2647                                         ExpressionPathEndResultType* final_result,
2648                                         const GetValueForExpressionPathOptions& options,
2649                                         ExpressionPathAftermath* what_next)
2650 {
2651     if (!root.get())
2652         return 0;
2653 
2654     *first_unparsed = expression_cstr;
2655 
2656     while (true)
2657     {
2658 
2659         const char* expression_cstr = *first_unparsed; // hide the top level expression_cstr
2660 
2661         clang_type_t root_clang_type = root->GetClangType();
2662         clang_type_t pointee_clang_type;
2663         Flags root_clang_type_info,pointee_clang_type_info;
2664 
2665         root_clang_type_info = Flags(ClangASTContext::GetTypeInfo(root_clang_type, GetClangAST(), &pointee_clang_type));
2666         if (pointee_clang_type)
2667             pointee_clang_type_info = Flags(ClangASTContext::GetTypeInfo(pointee_clang_type, GetClangAST(), NULL));
2668 
2669         if (!expression_cstr || *expression_cstr == '\0')
2670         {
2671             *reason_to_stop = ValueObject::eEndOfString;
2672             list->Append(root);
2673             return 1;
2674         }
2675 
2676         switch (*expression_cstr)
2677         {
2678             case '[':
2679             {
2680                 if (!root_clang_type_info.Test(ClangASTContext::eTypeIsArray) && !root_clang_type_info.Test(ClangASTContext::eTypeIsPointer)) // if this is not a T[] nor a T*
2681                 {
2682                     if (!root_clang_type_info.Test(ClangASTContext::eTypeIsScalar)) // if this is not even a scalar, this syntax is just plain wrong!
2683                     {
2684                         *first_unparsed = expression_cstr;
2685                         *reason_to_stop = ValueObject::eRangeOperatorInvalid;
2686                         *final_result = ValueObject::eInvalid;
2687                         return 0;
2688                     }
2689                     else if (!options.m_allow_bitfields_syntax) // if this is a scalar, check that we can expand bitfields
2690                     {
2691                         *first_unparsed = expression_cstr;
2692                         *reason_to_stop = ValueObject::eRangeOperatorNotAllowed;
2693                         *final_result = ValueObject::eInvalid;
2694                         return 0;
2695                     }
2696                 }
2697                 if (*(expression_cstr+1) == ']') // if this is an unbounded range it only works for arrays
2698                 {
2699                     if (!root_clang_type_info.Test(ClangASTContext::eTypeIsArray))
2700                     {
2701                         *first_unparsed = expression_cstr;
2702                         *reason_to_stop = ValueObject::eEmptyRangeNotAllowed;
2703                         *final_result = ValueObject::eInvalid;
2704                         return 0;
2705                     }
2706                     else // expand this into list
2707                     {
2708                         int max_index = root->GetNumChildren() - 1;
2709                         for (int index = 0; index < max_index; index++)
2710                         {
2711                             ValueObjectSP child =
2712                                 root->GetChildAtIndex(index, true);
2713                             list->Append(child);
2714                         }
2715                         *first_unparsed = expression_cstr+2;
2716                         *reason_to_stop = ValueObject::eRangeOperatorExpanded;
2717                         *final_result = ValueObject::eValueObjectList;
2718                         return max_index; // tell me number of items I added to the VOList
2719                     }
2720                 }
2721                 const char *separator_position = ::strchr(expression_cstr+1,'-');
2722                 const char *close_bracket_position = ::strchr(expression_cstr+1,']');
2723                 if (!close_bracket_position) // if there is no ], this is a syntax error
2724                 {
2725                     *first_unparsed = expression_cstr;
2726                     *reason_to_stop = ValueObject::eUnexpectedSymbol;
2727                     *final_result = ValueObject::eInvalid;
2728                     return 0;
2729                 }
2730                 if (!separator_position || separator_position > close_bracket_position) // if no separator, this is either [] or [N]
2731                 {
2732                     char *end = NULL;
2733                     unsigned long index = ::strtoul (expression_cstr+1, &end, 0);
2734                     if (!end || end != close_bracket_position) // if something weird is in our way return an error
2735                     {
2736                         *first_unparsed = expression_cstr;
2737                         *reason_to_stop = ValueObject::eUnexpectedSymbol;
2738                         *final_result = ValueObject::eInvalid;
2739                         return 0;
2740                     }
2741                     if (end - expression_cstr == 1) // if this is [], only return a valid value for arrays
2742                     {
2743                         if (root_clang_type_info.Test(ClangASTContext::eTypeIsArray))
2744                         {
2745                             int max_index = root->GetNumChildren() - 1;
2746                             for (int index = 0; index < max_index; index++)
2747                             {
2748                                 ValueObjectSP child =
2749                                 root->GetChildAtIndex(index, true);
2750                                 list->Append(child);
2751                             }
2752                             *first_unparsed = expression_cstr+2;
2753                             *reason_to_stop = ValueObject::eRangeOperatorExpanded;
2754                             *final_result = ValueObject::eValueObjectList;
2755                             return max_index; // tell me number of items I added to the VOList
2756                         }
2757                         else
2758                         {
2759                             *first_unparsed = expression_cstr;
2760                             *reason_to_stop = ValueObject::eEmptyRangeNotAllowed;
2761                             *final_result = ValueObject::eInvalid;
2762                             return 0;
2763                         }
2764                     }
2765                     // from here on we do have a valid index
2766                     if (root_clang_type_info.Test(ClangASTContext::eTypeIsArray))
2767                     {
2768                         root = root->GetChildAtIndex(index, true);
2769                         if (!root.get())
2770                         {
2771                             *first_unparsed = expression_cstr;
2772                             *reason_to_stop = ValueObject::eNoSuchChild;
2773                             *final_result = ValueObject::eInvalid;
2774                             return 0;
2775                         }
2776                         else
2777                         {
2778                             list->Append(root);
2779                             *first_unparsed = end+1; // skip ]
2780                             *reason_to_stop = ValueObject::eRangeOperatorExpanded;
2781                             *final_result = ValueObject::eValueObjectList;
2782                             return 1;
2783                         }
2784                     }
2785                     else if (root_clang_type_info.Test(ClangASTContext::eTypeIsPointer))
2786                     {
2787                         if (*what_next == ValueObject::eDereference &&  // if this is a ptr-to-scalar, I am accessing it by index and I would have deref'ed anyway, then do it now and use this as a bitfield
2788                             pointee_clang_type_info.Test(ClangASTContext::eTypeIsScalar))
2789                         {
2790                             Error error;
2791                             root = root->Dereference(error);
2792                             if (error.Fail() || !root.get())
2793                             {
2794                                 *first_unparsed = expression_cstr;
2795                                 *reason_to_stop = ValueObject::eDereferencingFailed;
2796                                 *final_result = ValueObject::eInvalid;
2797                                 return 0;
2798                             }
2799                             else
2800                             {
2801                                 *what_next = eNothing;
2802                                 continue;
2803                             }
2804                         }
2805                         else
2806                         {
2807                             root = root->GetSyntheticArrayMemberFromPointer(index, true);
2808                             if (!root.get())
2809                             {
2810                                 *first_unparsed = expression_cstr;
2811                                 *reason_to_stop = ValueObject::eNoSuchChild;
2812                                 *final_result = ValueObject::eInvalid;
2813                                 return 0;
2814                             }
2815                             else
2816                             {
2817                                 list->Append(root);
2818                                 *first_unparsed = end+1; // skip ]
2819                                 *reason_to_stop = ValueObject::eRangeOperatorExpanded;
2820                                 *final_result = ValueObject::eValueObjectList;
2821                                 return 1;
2822                             }
2823                         }
2824                     }
2825                     else /*if (ClangASTContext::IsScalarType(root_clang_type))*/
2826                     {
2827                         root = root->GetSyntheticBitFieldChild(index, index, true);
2828                         if (!root.get())
2829                         {
2830                             *first_unparsed = expression_cstr;
2831                             *reason_to_stop = ValueObject::eNoSuchChild;
2832                             *final_result = ValueObject::eInvalid;
2833                             return 0;
2834                         }
2835                         else // we do not know how to expand members of bitfields, so we just return and let the caller do any further processing
2836                         {
2837                             list->Append(root);
2838                             *first_unparsed = end+1; // skip ]
2839                             *reason_to_stop = ValueObject::eRangeOperatorExpanded;
2840                             *final_result = ValueObject::eValueObjectList;
2841                             return 1;
2842                         }
2843                     }
2844                 }
2845                 else // we have a low and a high index
2846                 {
2847                     char *end = NULL;
2848                     unsigned long index_lower = ::strtoul (expression_cstr+1, &end, 0);
2849                     if (!end || end != separator_position) // if something weird is in our way return an error
2850                     {
2851                         *first_unparsed = expression_cstr;
2852                         *reason_to_stop = ValueObject::eUnexpectedSymbol;
2853                         *final_result = ValueObject::eInvalid;
2854                         return 0;
2855                     }
2856                     unsigned long index_higher = ::strtoul (separator_position+1, &end, 0);
2857                     if (!end || end != close_bracket_position) // if something weird is in our way return an error
2858                     {
2859                         *first_unparsed = expression_cstr;
2860                         *reason_to_stop = ValueObject::eUnexpectedSymbol;
2861                         *final_result = ValueObject::eInvalid;
2862                         return 0;
2863                     }
2864                     if (index_lower > index_higher) // swap indices if required
2865                     {
2866                         unsigned long temp = index_lower;
2867                         index_lower = index_higher;
2868                         index_higher = temp;
2869                     }
2870                     if (root_clang_type_info.Test(ClangASTContext::eTypeIsScalar)) // expansion only works for scalars
2871                     {
2872                         root = root->GetSyntheticBitFieldChild(index_lower, index_higher, true);
2873                         if (!root.get())
2874                         {
2875                             *first_unparsed = expression_cstr;
2876                             *reason_to_stop = ValueObject::eNoSuchChild;
2877                             *final_result = ValueObject::eInvalid;
2878                             return 0;
2879                         }
2880                         else
2881                         {
2882                             list->Append(root);
2883                             *first_unparsed = end+1; // skip ]
2884                             *reason_to_stop = ValueObject::eRangeOperatorExpanded;
2885                             *final_result = ValueObject::eValueObjectList;
2886                             return 1;
2887                         }
2888                     }
2889                     else if (root_clang_type_info.Test(ClangASTContext::eTypeIsPointer) && // if this is a ptr-to-scalar, I am accessing it by index and I would have deref'ed anyway, then do it now and use this as a bitfield
2890                              *what_next == ValueObject::eDereference &&
2891                              pointee_clang_type_info.Test(ClangASTContext::eTypeIsScalar))
2892                     {
2893                         Error error;
2894                         root = root->Dereference(error);
2895                         if (error.Fail() || !root.get())
2896                         {
2897                             *first_unparsed = expression_cstr;
2898                             *reason_to_stop = ValueObject::eDereferencingFailed;
2899                             *final_result = ValueObject::eInvalid;
2900                             return 0;
2901                         }
2902                         else
2903                         {
2904                             *what_next = ValueObject::eNothing;
2905                             continue;
2906                         }
2907                     }
2908                     else
2909                     {
2910                         for (unsigned long index = index_lower;
2911                              index <= index_higher; index++)
2912                         {
2913                             ValueObjectSP child =
2914                                 root->GetChildAtIndex(index, true);
2915                             list->Append(child);
2916                         }
2917                         *first_unparsed = end+1;
2918                         *reason_to_stop = ValueObject::eRangeOperatorExpanded;
2919                         *final_result = ValueObject::eValueObjectList;
2920                         return index_higher-index_lower+1; // tell me number of items I added to the VOList
2921                     }
2922                 }
2923                 break;
2924             }
2925             default: // some non-[ separator, or something entirely wrong, is in the way
2926             {
2927                 *first_unparsed = expression_cstr;
2928                 *reason_to_stop = ValueObject::eUnexpectedSymbol;
2929                 *final_result = ValueObject::eInvalid;
2930                 return 0;
2931                 break;
2932             }
2933         }
2934     }
2935 }
2936 
2937 void
2938 ValueObject::DumpValueObject
2939 (
2940     Stream &s,
2941     ValueObject *valobj,
2942     const char *root_valobj_name,
2943     uint32_t ptr_depth,
2944     uint32_t curr_depth,
2945     uint32_t max_depth,
2946     bool show_types,
2947     bool show_location,
2948     bool use_objc,
2949     DynamicValueType use_dynamic,
2950     bool use_synth,
2951     bool scope_already_checked,
2952     bool flat_output,
2953     uint32_t omit_summary_depth,
2954     bool ignore_cap
2955 )
2956 {
2957     if (valobj)
2958     {
2959         bool update_success = valobj->UpdateValueIfNeeded (use_dynamic, true);
2960 
2961         if (update_success && use_dynamic != eNoDynamicValues)
2962         {
2963             ValueObject *dynamic_value = valobj->GetDynamicValue(use_dynamic).get();
2964             if (dynamic_value)
2965                 valobj = dynamic_value;
2966         }
2967 
2968         clang_type_t clang_type = valobj->GetClangType();
2969 
2970         const Flags type_flags (ClangASTContext::GetTypeInfo (clang_type, NULL, NULL));
2971         const char *err_cstr = NULL;
2972         const bool has_children = type_flags.Test (ClangASTContext::eTypeHasChildren);
2973         const bool has_value = type_flags.Test (ClangASTContext::eTypeHasValue);
2974 
2975         const bool print_valobj = flat_output == false || has_value;
2976 
2977         if (print_valobj)
2978         {
2979             if (show_location)
2980             {
2981                 s.Printf("%s: ", valobj->GetLocationAsCString());
2982             }
2983 
2984             s.Indent();
2985 
2986             // Always show the type for the top level items.
2987             if (show_types || (curr_depth == 0 && !flat_output))
2988             {
2989                 const char* typeName = valobj->GetTypeName().AsCString("<invalid type>");
2990                 s.Printf("(%s", typeName);
2991                 // only show dynamic types if the user really wants to see types
2992                 if (show_types && use_dynamic != eNoDynamicValues &&
2993                     (/*strstr(typeName, "id") == typeName ||*/
2994                      ClangASTType::GetMinimumLanguage(valobj->GetClangAST(), valobj->GetClangType()) == eLanguageTypeObjC))
2995                 {
2996                     Process* process = valobj->GetUpdatePoint().GetProcessSP().get();
2997                     if (process == NULL)
2998                         s.Printf(", dynamic type: unknown) ");
2999                     else
3000                     {
3001                         ObjCLanguageRuntime *runtime = process->GetObjCLanguageRuntime();
3002                         if (runtime == NULL)
3003                             s.Printf(", dynamic type: unknown) ");
3004                         else
3005                         {
3006                             ObjCLanguageRuntime::ObjCISA isa = runtime->GetISA(*valobj);
3007                             if (!runtime->IsValidISA(isa))
3008                                 s.Printf(", dynamic type: unknown) ");
3009                             else
3010                                 s.Printf(", dynamic type: %s) ",
3011                                          runtime->GetActualTypeName(isa).GetCString());
3012                         }
3013                     }
3014                 }
3015                 else
3016                     s.Printf(") ");
3017             }
3018 
3019 
3020             if (flat_output)
3021             {
3022                 // If we are showing types, also qualify the C++ base classes
3023                 const bool qualify_cxx_base_classes = show_types;
3024                 valobj->GetExpressionPath(s, qualify_cxx_base_classes);
3025                 s.PutCString(" =");
3026             }
3027             else
3028             {
3029                 const char *name_cstr = root_valobj_name ? root_valobj_name : valobj->GetName().AsCString("");
3030                 s.Printf ("%s =", name_cstr);
3031             }
3032 
3033             if (!scope_already_checked && !valobj->IsInScope())
3034             {
3035                 err_cstr = "out of scope";
3036             }
3037         }
3038 
3039         const char *val_cstr = NULL;
3040         const char *sum_cstr = NULL;
3041         SummaryFormat* entry = valobj->GetSummaryFormat().get();
3042 
3043         if (omit_summary_depth > 0)
3044             entry = NULL;
3045 
3046         if (err_cstr == NULL)
3047         {
3048             val_cstr = valobj->GetValueAsCString();
3049             err_cstr = valobj->GetError().AsCString();
3050         }
3051 
3052         if (err_cstr)
3053         {
3054             s.Printf (" <%s>\n", err_cstr);
3055         }
3056         else
3057         {
3058             const bool is_ref = type_flags.Test (ClangASTContext::eTypeIsReference);
3059             if (print_valobj)
3060             {
3061 
3062                 sum_cstr = (omit_summary_depth == 0) ? valobj->GetSummaryAsCString() : NULL;
3063 
3064                 // We must calculate this value in realtime because entry might alter this variable's value
3065                 // (e.g. by saying ${var%fmt}) and render precached values useless
3066                 if (val_cstr && (!entry || entry->DoesPrintValue() || !sum_cstr))
3067                     s.Printf(" %s", valobj->GetValueAsCString());
3068 
3069                 if (sum_cstr)
3070                 {
3071                     // for some reason, using %@ (ObjC description) in a summary string, makes
3072                     // us believe we need to reset ourselves, thus invalidating the content of
3073                     // sum_cstr. Thus, IF we had a valid sum_cstr before, but it is now empty
3074                     // let us recalculate it!
3075                     if (sum_cstr[0] == '\0')
3076                         s.Printf(" %s", valobj->GetSummaryAsCString());
3077                     else
3078                         s.Printf(" %s", sum_cstr);
3079                 }
3080 
3081                 if (use_objc)
3082                 {
3083                     const char *object_desc = valobj->GetObjectDescription();
3084                     if (object_desc)
3085                         s.Printf(" %s\n", object_desc);
3086                     else
3087                         s.Printf (" [no Objective-C description available]\n");
3088                     return;
3089                 }
3090             }
3091 
3092             if (curr_depth < max_depth)
3093             {
3094                 // We will show children for all concrete types. We won't show
3095                 // pointer contents unless a pointer depth has been specified.
3096                 // We won't reference contents unless the reference is the
3097                 // root object (depth of zero).
3098                 bool print_children = true;
3099 
3100                 // Use a new temporary pointer depth in case we override the
3101                 // current pointer depth below...
3102                 uint32_t curr_ptr_depth = ptr_depth;
3103 
3104                 const bool is_ptr = type_flags.Test (ClangASTContext::eTypeIsPointer);
3105                 if (is_ptr || is_ref)
3106                 {
3107                     // We have a pointer or reference whose value is an address.
3108                     // Make sure that address is not NULL
3109                     AddressType ptr_address_type;
3110                     if (valobj->GetPointerValue (&ptr_address_type) == 0)
3111                         print_children = false;
3112 
3113                     else if (is_ref && curr_depth == 0)
3114                     {
3115                         // If this is the root object (depth is zero) that we are showing
3116                         // and it is a reference, and no pointer depth has been supplied
3117                         // print out what it references. Don't do this at deeper depths
3118                         // otherwise we can end up with infinite recursion...
3119                         curr_ptr_depth = 1;
3120                     }
3121 
3122                     if (curr_ptr_depth == 0)
3123                         print_children = false;
3124                 }
3125 
3126                 if (print_children && (!entry || entry->DoesPrintChildren() || !sum_cstr))
3127                 {
3128                     ValueObjectSP synth_valobj = valobj->GetSyntheticValue(use_synth ?
3129                                                                          eUseSyntheticFilter :
3130                                                                          eNoSyntheticFilter);
3131                     uint32_t num_children = synth_valobj->GetNumChildren();
3132                     bool print_dotdotdot = false;
3133                     if (num_children)
3134                     {
3135                         if (flat_output)
3136                         {
3137                             if (print_valobj)
3138                                 s.EOL();
3139                         }
3140                         else
3141                         {
3142                             if (print_valobj)
3143                                 s.PutCString(is_ref ? ": {\n" : " {\n");
3144                             s.IndentMore();
3145                         }
3146 
3147                         uint32_t max_num_children = valobj->GetUpdatePoint().GetTargetSP()->GetMaximumNumberOfChildrenToDisplay();
3148 
3149                         if (num_children > max_num_children && !ignore_cap)
3150                         {
3151                             num_children = max_num_children;
3152                             print_dotdotdot = true;
3153                         }
3154 
3155                         for (uint32_t idx=0; idx<num_children; ++idx)
3156                         {
3157                             ValueObjectSP child_sp(synth_valobj->GetChildAtIndex(idx, true));
3158                             if (child_sp.get())
3159                             {
3160                                 DumpValueObject (s,
3161                                                  child_sp.get(),
3162                                                  NULL,
3163                                                  (is_ptr || is_ref) ? curr_ptr_depth - 1 : curr_ptr_depth,
3164                                                  curr_depth + 1,
3165                                                  max_depth,
3166                                                  show_types,
3167                                                  show_location,
3168                                                  false,
3169                                                  use_dynamic,
3170                                                  use_synth,
3171                                                  true,
3172                                                  flat_output,
3173                                                  omit_summary_depth > 1 ? omit_summary_depth - 1 : 0,
3174                                                  ignore_cap);
3175                             }
3176                         }
3177 
3178                         if (!flat_output)
3179                         {
3180                             if (print_dotdotdot)
3181                             {
3182                                 valobj->GetUpdatePoint().GetTargetSP()->GetDebugger().GetCommandInterpreter().ChildrenTruncated();
3183                                 s.Indent("...\n");
3184                             }
3185                             s.IndentLess();
3186                             s.Indent("}\n");
3187                         }
3188                     }
3189                     else if (has_children)
3190                     {
3191                         // Aggregate, no children...
3192                         if (print_valobj)
3193                             s.PutCString(" {}\n");
3194                     }
3195                     else
3196                     {
3197                         if (print_valobj)
3198                             s.EOL();
3199                     }
3200 
3201                 }
3202                 else
3203                 {
3204                     s.EOL();
3205                 }
3206             }
3207             else
3208             {
3209                 if (has_children && print_valobj)
3210                 {
3211                     s.PutCString("{...}\n");
3212                 }
3213             }
3214         }
3215     }
3216 }
3217 
3218 
3219 ValueObjectSP
3220 ValueObject::CreateConstantValue (const ConstString &name)
3221 {
3222     ValueObjectSP valobj_sp;
3223 
3224     if (UpdateValueIfNeeded(false) && m_error.Success())
3225     {
3226         ExecutionContextScope *exe_scope = GetExecutionContextScope();
3227         if (exe_scope)
3228         {
3229             ExecutionContext exe_ctx;
3230             exe_scope->CalculateExecutionContext(exe_ctx);
3231 
3232             clang::ASTContext *ast = GetClangAST ();
3233 
3234             DataExtractor data;
3235             data.SetByteOrder (m_data.GetByteOrder());
3236             data.SetAddressByteSize(m_data.GetAddressByteSize());
3237 
3238             m_error = m_value.GetValueAsData (&exe_ctx, ast, data, 0, GetModule());
3239 
3240             valobj_sp = ValueObjectConstResult::Create (exe_scope,
3241                                                         ast,
3242                                                         GetClangType(),
3243                                                         name,
3244                                                         data,
3245 													    GetAddressOf());
3246         }
3247     }
3248 
3249     if (!valobj_sp)
3250     {
3251         valobj_sp = ValueObjectConstResult::Create (NULL, m_error);
3252     }
3253     return valobj_sp;
3254 }
3255 
3256 ValueObjectSP
3257 ValueObject::Dereference (Error &error)
3258 {
3259     if (m_deref_valobj)
3260         return m_deref_valobj->GetSP();
3261 
3262     const bool is_pointer_type = IsPointerType();
3263     if (is_pointer_type)
3264     {
3265         bool omit_empty_base_classes = true;
3266         bool ignore_array_bounds = false;
3267 
3268         std::string child_name_str;
3269         uint32_t child_byte_size = 0;
3270         int32_t child_byte_offset = 0;
3271         uint32_t child_bitfield_bit_size = 0;
3272         uint32_t child_bitfield_bit_offset = 0;
3273         bool child_is_base_class = false;
3274         bool child_is_deref_of_parent = false;
3275         const bool transparent_pointers = false;
3276         clang::ASTContext *clang_ast = GetClangAST();
3277         clang_type_t clang_type = GetClangType();
3278         clang_type_t child_clang_type;
3279 
3280         ExecutionContext exe_ctx;
3281         GetExecutionContextScope()->CalculateExecutionContext (exe_ctx);
3282 
3283         child_clang_type = ClangASTContext::GetChildClangTypeAtIndex (&exe_ctx,
3284                                                                       clang_ast,
3285                                                                       GetName().GetCString(),
3286                                                                       clang_type,
3287                                                                       0,
3288                                                                       transparent_pointers,
3289                                                                       omit_empty_base_classes,
3290                                                                       ignore_array_bounds,
3291                                                                       child_name_str,
3292                                                                       child_byte_size,
3293                                                                       child_byte_offset,
3294                                                                       child_bitfield_bit_size,
3295                                                                       child_bitfield_bit_offset,
3296                                                                       child_is_base_class,
3297                                                                       child_is_deref_of_parent);
3298         if (child_clang_type && child_byte_size)
3299         {
3300             ConstString child_name;
3301             if (!child_name_str.empty())
3302                 child_name.SetCString (child_name_str.c_str());
3303 
3304             m_deref_valobj = new ValueObjectChild (*this,
3305                                                    clang_ast,
3306                                                    child_clang_type,
3307                                                    child_name,
3308                                                    child_byte_size,
3309                                                    child_byte_offset,
3310                                                    child_bitfield_bit_size,
3311                                                    child_bitfield_bit_offset,
3312                                                    child_is_base_class,
3313                                                    child_is_deref_of_parent,
3314                                                    eAddressTypeInvalid);
3315         }
3316     }
3317 
3318     if (m_deref_valobj)
3319     {
3320         error.Clear();
3321         return m_deref_valobj->GetSP();
3322     }
3323     else
3324     {
3325         StreamString strm;
3326         GetExpressionPath(strm, true);
3327 
3328         if (is_pointer_type)
3329             error.SetErrorStringWithFormat("dereference failed: (%s) %s", GetTypeName().AsCString("<invalid type>"), strm.GetString().c_str());
3330         else
3331             error.SetErrorStringWithFormat("not a pointer type: (%s) %s", GetTypeName().AsCString("<invalid type>"), strm.GetString().c_str());
3332         return ValueObjectSP();
3333     }
3334 }
3335 
3336 ValueObjectSP
3337 ValueObject::AddressOf (Error &error)
3338 {
3339     if (m_addr_of_valobj_sp)
3340         return m_addr_of_valobj_sp;
3341 
3342     AddressType address_type = eAddressTypeInvalid;
3343     const bool scalar_is_load_address = false;
3344     addr_t addr = GetAddressOf (scalar_is_load_address, &address_type);
3345     error.Clear();
3346     if (addr != LLDB_INVALID_ADDRESS)
3347     {
3348         switch (address_type)
3349         {
3350         default:
3351         case eAddressTypeInvalid:
3352             {
3353                 StreamString expr_path_strm;
3354                 GetExpressionPath(expr_path_strm, true);
3355                 error.SetErrorStringWithFormat("'%s' is not in memory", expr_path_strm.GetString().c_str());
3356             }
3357             break;
3358 
3359         case eAddressTypeFile:
3360         case eAddressTypeLoad:
3361         case eAddressTypeHost:
3362             {
3363                 clang::ASTContext *ast = GetClangAST();
3364                 clang_type_t clang_type = GetClangType();
3365                 if (ast && clang_type)
3366                 {
3367                     std::string name (1, '&');
3368                     name.append (m_name.AsCString(""));
3369                     m_addr_of_valobj_sp = ValueObjectConstResult::Create (GetExecutionContextScope(),
3370                                                                           ast,
3371                                                                           ClangASTContext::CreatePointerType (ast, clang_type),
3372                                                                           ConstString (name.c_str()),
3373                                                                           addr,
3374                                                                           eAddressTypeInvalid,
3375                                                                           m_data.GetAddressByteSize());
3376                 }
3377             }
3378             break;
3379         }
3380     }
3381     return m_addr_of_valobj_sp;
3382 }
3383 
3384 
3385 ValueObjectSP
3386 ValueObject::CastPointerType (const char *name, ClangASTType &clang_ast_type)
3387 {
3388     ValueObjectSP valobj_sp;
3389     AddressType address_type;
3390     addr_t ptr_value = GetPointerValue (&address_type);
3391 
3392     if (ptr_value != LLDB_INVALID_ADDRESS)
3393     {
3394         Address ptr_addr (NULL, ptr_value);
3395 
3396         valobj_sp = ValueObjectMemory::Create (GetExecutionContextScope(),
3397                                                name,
3398                                                ptr_addr,
3399                                                clang_ast_type);
3400     }
3401     return valobj_sp;
3402 }
3403 
3404 ValueObjectSP
3405 ValueObject::CastPointerType (const char *name, TypeSP &type_sp)
3406 {
3407     ValueObjectSP valobj_sp;
3408     AddressType address_type;
3409     addr_t ptr_value = GetPointerValue (&address_type);
3410 
3411     if (ptr_value != LLDB_INVALID_ADDRESS)
3412     {
3413         Address ptr_addr (NULL, ptr_value);
3414 
3415         valobj_sp = ValueObjectMemory::Create (GetExecutionContextScope(),
3416                                                name,
3417                                                ptr_addr,
3418                                                type_sp);
3419     }
3420     return valobj_sp;
3421 }
3422 
3423 ValueObject::EvaluationPoint::EvaluationPoint () :
3424     m_thread_id (LLDB_INVALID_UID),
3425     m_mod_id ()
3426 {
3427 }
3428 
3429 ValueObject::EvaluationPoint::EvaluationPoint (ExecutionContextScope *exe_scope, bool use_selected):
3430     m_needs_update (true),
3431     m_first_update (true),
3432     m_thread_id (LLDB_INVALID_THREAD_ID),
3433     m_mod_id ()
3434 
3435 {
3436     ExecutionContext exe_ctx;
3437     ExecutionContextScope *computed_exe_scope = exe_scope;  // If use_selected is true, we may find a better scope,
3438                                                             // and if so we want to cache that not the original.
3439     if (exe_scope)
3440         exe_scope->CalculateExecutionContext(exe_ctx);
3441     Target *target = exe_ctx.GetTargetPtr();
3442     if (target != NULL)
3443     {
3444         m_target_sp = target;
3445         m_process_sp = exe_ctx.GetProcessSP();
3446         if (!m_process_sp)
3447             m_process_sp = target->GetProcessSP();
3448 
3449         if (m_process_sp)
3450         {
3451             m_mod_id = m_process_sp->GetModID();
3452 
3453             Thread *thread = exe_ctx.GetThreadPtr();
3454 
3455             if (thread == NULL)
3456             {
3457                 if (use_selected)
3458                 {
3459                     thread = m_process_sp->GetThreadList().GetSelectedThread().get();
3460                     if (thread)
3461                         computed_exe_scope = thread;
3462                 }
3463             }
3464 
3465             if (thread != NULL)
3466             {
3467                 m_thread_id = thread->GetIndexID();
3468 
3469                 StackFrame *frame = exe_ctx.GetFramePtr();
3470                 if (frame == NULL)
3471                 {
3472                     if (use_selected)
3473                     {
3474                         frame = thread->GetSelectedFrame().get();
3475                         if (frame)
3476                         {
3477                             m_stack_id = frame->GetStackID();
3478                             computed_exe_scope = frame;
3479                         }
3480                     }
3481                 }
3482                 else
3483                     m_stack_id = frame->GetStackID();
3484             }
3485         }
3486     }
3487     m_exe_scope = computed_exe_scope;
3488 }
3489 
3490 ValueObject::EvaluationPoint::EvaluationPoint (const ValueObject::EvaluationPoint &rhs) :
3491     m_exe_scope (rhs.m_exe_scope),
3492     m_needs_update(true),
3493     m_first_update(true),
3494     m_target_sp (rhs.m_target_sp),
3495     m_process_sp (rhs.m_process_sp),
3496     m_thread_id (rhs.m_thread_id),
3497     m_stack_id (rhs.m_stack_id),
3498     m_mod_id ()
3499 {
3500 }
3501 
3502 ValueObject::EvaluationPoint::~EvaluationPoint ()
3503 {
3504 }
3505 
3506 ExecutionContextScope *
3507 ValueObject::EvaluationPoint::GetExecutionContextScope ()
3508 {
3509     // We have to update before giving out the scope, or we could be handing out stale pointers.
3510     SyncWithProcessState();
3511 
3512     return m_exe_scope;
3513 }
3514 
3515 // This function checks the EvaluationPoint against the current process state.  If the current
3516 // state matches the evaluation point, or the evaluation point is already invalid, then we return
3517 // false, meaning "no change".  If the current state is different, we update our state, and return
3518 // true meaning "yes, change".  If we did see a change, we also set m_needs_update to true, so
3519 // future calls to NeedsUpdate will return true.
3520 
3521 bool
3522 ValueObject::EvaluationPoint::SyncWithProcessState()
3523 {
3524     // If we don't have a process nothing can change.
3525     if (!m_process_sp)
3526     {
3527         m_exe_scope = m_target_sp.get();
3528         return false;
3529     }
3530 
3531     // If our stop id is the current stop ID, nothing has changed:
3532     ProcessModID current_mod_id = m_process_sp->GetModID();
3533 
3534     // If the current stop id is 0, either we haven't run yet, or the process state has been cleared.
3535     // In either case, we aren't going to be able to sync with the process state.
3536     if (current_mod_id.GetStopID() == 0)
3537     {
3538         m_exe_scope = m_target_sp.get();
3539         return false;
3540     }
3541 
3542     if (m_mod_id.IsValid())
3543     {
3544         if (m_mod_id == current_mod_id)
3545         {
3546             // Everything is already up to date in this object, no need do
3547             // update the execution context scope.
3548             return false;
3549         }
3550         m_mod_id = current_mod_id;
3551         m_needs_update = true;
3552     }
3553     m_exe_scope = m_process_sp.get();
3554 
3555     // Something has changed, so we will return true.  Now make sure the thread & frame still exist, and if either
3556     // doesn't, mark ourselves as invalid.
3557 
3558     if (m_thread_id != LLDB_INVALID_THREAD_ID)
3559     {
3560         Thread *our_thread = m_process_sp->GetThreadList().FindThreadByIndexID (m_thread_id).get();
3561         if (our_thread == NULL)
3562         {
3563             SetInvalid();
3564         }
3565         else
3566         {
3567             m_exe_scope = our_thread;
3568 
3569             if (m_stack_id.IsValid())
3570             {
3571                 StackFrame *our_frame = our_thread->GetFrameWithStackID (m_stack_id).get();
3572                 if (our_frame == NULL)
3573                     SetInvalid();
3574                 else
3575                     m_exe_scope = our_frame;
3576             }
3577         }
3578     }
3579     return true;
3580 }
3581 
3582 void
3583 ValueObject::EvaluationPoint::SetUpdated ()
3584 {
3585     // this will update the execution context scope and the m_mod_id
3586     SyncWithProcessState();
3587     m_first_update = false;
3588     m_needs_update = false;
3589 }
3590 
3591 
3592 bool
3593 ValueObject::EvaluationPoint::SetContext (ExecutionContextScope *exe_scope)
3594 {
3595     if (!IsValid())
3596         return false;
3597 
3598     bool needs_update = false;
3599     m_exe_scope = NULL;
3600 
3601     // The target has to be non-null, and the
3602     Target *target = exe_scope->CalculateTarget();
3603     if (target != NULL)
3604     {
3605         Target *old_target = m_target_sp.get();
3606         assert (target == old_target);
3607         Process *process = exe_scope->CalculateProcess();
3608         if (process != NULL)
3609         {
3610             // FOR NOW - assume you can't update variable objects across process boundaries.
3611             Process *old_process = m_process_sp.get();
3612             assert (process == old_process);
3613             ProcessModID current_mod_id = process->GetModID();
3614             if (m_mod_id != current_mod_id)
3615             {
3616                 needs_update = true;
3617                 m_mod_id = current_mod_id;
3618             }
3619             // See if we're switching the thread or stack context.  If no thread is given, this is
3620             // being evaluated in a global context.
3621             Thread *thread = exe_scope->CalculateThread();
3622             if (thread != NULL)
3623             {
3624                 user_id_t new_thread_index = thread->GetIndexID();
3625                 if (new_thread_index != m_thread_id)
3626                 {
3627                     needs_update = true;
3628                     m_thread_id = new_thread_index;
3629                     m_stack_id.Clear();
3630                 }
3631 
3632                 StackFrame *new_frame = exe_scope->CalculateStackFrame();
3633                 if (new_frame != NULL)
3634                 {
3635                     if (new_frame->GetStackID() != m_stack_id)
3636                     {
3637                         needs_update = true;
3638                         m_stack_id = new_frame->GetStackID();
3639                     }
3640                 }
3641                 else
3642                 {
3643                     m_stack_id.Clear();
3644                     needs_update = true;
3645                 }
3646             }
3647             else
3648             {
3649                 // If this had been given a thread, and now there is none, we should update.
3650                 // Otherwise we don't have to do anything.
3651                 if (m_thread_id != LLDB_INVALID_UID)
3652                 {
3653                     m_thread_id = LLDB_INVALID_UID;
3654                     m_stack_id.Clear();
3655                     needs_update = true;
3656                 }
3657             }
3658         }
3659         else
3660         {
3661             // If there is no process, then we don't need to update anything.
3662             // But if we're switching from having a process to not, we should try to update.
3663             if (m_process_sp.get() != NULL)
3664             {
3665                 needs_update = true;
3666                 m_process_sp.reset();
3667                 m_thread_id = LLDB_INVALID_UID;
3668                 m_stack_id.Clear();
3669             }
3670         }
3671     }
3672     else
3673     {
3674         // If there's no target, nothing can change so we don't need to update anything.
3675         // But if we're switching from having a target to not, we should try to update.
3676         if (m_target_sp.get() != NULL)
3677         {
3678             needs_update = true;
3679             m_target_sp.reset();
3680             m_process_sp.reset();
3681             m_thread_id = LLDB_INVALID_UID;
3682             m_stack_id.Clear();
3683         }
3684     }
3685     if (!m_needs_update)
3686         m_needs_update = needs_update;
3687 
3688     return needs_update;
3689 }
3690 
3691 void
3692 ValueObject::ClearUserVisibleData()
3693 {
3694     m_location_str.clear();
3695     m_value_str.clear();
3696     m_summary_str.clear();
3697     m_object_desc_str.clear();
3698     m_trying_summary_already = false;
3699 }
3700 
3701 SymbolContextScope *
3702 ValueObject::GetSymbolContextScope()
3703 {
3704     if (m_parent)
3705     {
3706         if (!m_parent->IsPointerOrReferenceType())
3707             return m_parent->GetSymbolContextScope();
3708     }
3709     return NULL;
3710 }
3711