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 
19 // Project includes
20 #include "lldb/Core/DataBufferHeap.h"
21 #include "lldb/Core/Debugger.h"
22 #include "lldb/Core/Log.h"
23 #include "lldb/Core/Module.h"
24 #include "lldb/Core/StreamString.h"
25 #include "lldb/Core/ValueObjectCast.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/DataFormatters/DataVisualization.h"
34 #include "lldb/DataFormatters/StringPrinter.h"
35 #include "lldb/DataFormatters/ValueObjectPrinter.h"
36 
37 #include "Plugins/ExpressionParser/Clang/ClangExpressionVariable.h"
38 #include "Plugins/ExpressionParser/Clang/ClangPersistentVariables.h"
39 
40 #include "lldb/Host/Endian.h"
41 
42 #include "lldb/Interpreter/CommandInterpreter.h"
43 
44 #include "lldb/Symbol/CompilerType.h"
45 #include "lldb/Symbol/ClangASTContext.h"
46 #include "lldb/Symbol/CompileUnit.h"
47 #include "lldb/Symbol/Type.h"
48 
49 #include "lldb/Target/ExecutionContext.h"
50 #include "lldb/Target/LanguageRuntime.h"
51 #include "lldb/Target/ObjCLanguageRuntime.h"
52 #include "lldb/Target/Process.h"
53 #include "lldb/Target/RegisterContext.h"
54 #include "lldb/Target/SectionLoadList.h"
55 #include "lldb/Target/Target.h"
56 #include "lldb/Target/Thread.h"
57 
58 using namespace lldb;
59 using namespace lldb_private;
60 using namespace lldb_utility;
61 
62 static user_id_t g_value_obj_uid = 0;
63 
64 //----------------------------------------------------------------------
65 // ValueObject constructor
66 //----------------------------------------------------------------------
67 ValueObject::ValueObject (ValueObject &parent) :
68     UserID (++g_value_obj_uid), // Unique identifier for every value object
69     m_parent (&parent),
70     m_root (NULL),
71     m_update_point (parent.GetUpdatePoint ()),
72     m_name (),
73     m_data (),
74     m_value (),
75     m_error (),
76     m_value_str (),
77     m_old_value_str (),
78     m_location_str (),
79     m_summary_str (),
80     m_object_desc_str (),
81     m_validation_result(),
82     m_manager(parent.GetManager()),
83     m_children (),
84     m_synthetic_children (),
85     m_dynamic_value (NULL),
86     m_synthetic_value(NULL),
87     m_deref_valobj(NULL),
88     m_format (eFormatDefault),
89     m_last_format (eFormatDefault),
90     m_last_format_mgr_revision(0),
91     m_type_summary_sp(),
92     m_type_format_sp(),
93     m_synthetic_children_sp(),
94     m_type_validator_sp(),
95     m_user_id_of_forced_summary(),
96     m_address_type_of_ptr_or_ref_children(eAddressTypeInvalid),
97     m_value_checksum(),
98     m_preferred_display_language(lldb::eLanguageTypeUnknown),
99     m_value_is_valid (false),
100     m_value_did_change (false),
101     m_children_count_valid (false),
102     m_old_value_valid (false),
103     m_is_deref_of_parent (false),
104     m_is_array_item_for_pointer(false),
105     m_is_bitfield_for_scalar(false),
106     m_is_child_at_offset(false),
107     m_is_getting_summary(false),
108     m_did_calculate_complete_objc_class_type(false),
109     m_is_synthetic_children_generated(parent.m_is_synthetic_children_generated)
110 {
111     m_manager->ManageObject(this);
112 }
113 
114 //----------------------------------------------------------------------
115 // ValueObject constructor
116 //----------------------------------------------------------------------
117 ValueObject::ValueObject (ExecutionContextScope *exe_scope,
118                           AddressType child_ptr_or_ref_addr_type) :
119     UserID (++g_value_obj_uid), // Unique identifier for every value object
120     m_parent (NULL),
121     m_root (NULL),
122     m_update_point (exe_scope),
123     m_name (),
124     m_data (),
125     m_value (),
126     m_error (),
127     m_value_str (),
128     m_old_value_str (),
129     m_location_str (),
130     m_summary_str (),
131     m_object_desc_str (),
132     m_validation_result(),
133     m_manager(),
134     m_children (),
135     m_synthetic_children (),
136     m_dynamic_value (NULL),
137     m_synthetic_value(NULL),
138     m_deref_valobj(NULL),
139     m_format (eFormatDefault),
140     m_last_format (eFormatDefault),
141     m_last_format_mgr_revision(0),
142     m_type_summary_sp(),
143     m_type_format_sp(),
144     m_synthetic_children_sp(),
145     m_type_validator_sp(),
146     m_user_id_of_forced_summary(),
147     m_address_type_of_ptr_or_ref_children(child_ptr_or_ref_addr_type),
148     m_value_checksum(),
149     m_preferred_display_language(lldb::eLanguageTypeUnknown),
150     m_value_is_valid (false),
151     m_value_did_change (false),
152     m_children_count_valid (false),
153     m_old_value_valid (false),
154     m_is_deref_of_parent (false),
155     m_is_array_item_for_pointer(false),
156     m_is_bitfield_for_scalar(false),
157     m_is_child_at_offset(false),
158     m_is_getting_summary(false),
159     m_did_calculate_complete_objc_class_type(false),
160     m_is_synthetic_children_generated(false)
161 {
162     m_manager = new ValueObjectManager();
163     m_manager->ManageObject (this);
164 }
165 
166 //----------------------------------------------------------------------
167 // Destructor
168 //----------------------------------------------------------------------
169 ValueObject::~ValueObject ()
170 {
171 }
172 
173 bool
174 ValueObject::UpdateValueIfNeeded (bool update_format)
175 {
176 
177     bool did_change_formats = false;
178 
179     if (update_format)
180         did_change_formats = UpdateFormatsIfNeeded();
181 
182     // If this is a constant value, then our success is predicated on whether
183     // we have an error or not
184     if (GetIsConstant())
185     {
186         // if you are constant, things might still have changed behind your back
187         // (e.g. you are a frozen object and things have changed deeper than you cared to freeze-dry yourself)
188         // in this case, your value has not changed, but "computed" entries might have, so you might now have
189         // a different summary, or a different object description. clear these so we will recompute them
190         if (update_format && !did_change_formats)
191             ClearUserVisibleData(eClearUserVisibleDataItemsSummary | eClearUserVisibleDataItemsDescription);
192         return m_error.Success();
193     }
194 
195     bool first_update = IsChecksumEmpty();
196 
197     if (NeedsUpdating())
198     {
199         m_update_point.SetUpdated();
200 
201         // Save the old value using swap to avoid a string copy which
202         // also will clear our m_value_str
203         if (m_value_str.empty())
204         {
205             m_old_value_valid = false;
206         }
207         else
208         {
209             m_old_value_valid = true;
210             m_old_value_str.swap (m_value_str);
211             ClearUserVisibleData(eClearUserVisibleDataItemsValue);
212         }
213 
214         ClearUserVisibleData();
215 
216         if (IsInScope())
217         {
218             const bool value_was_valid = GetValueIsValid();
219             SetValueDidChange (false);
220 
221             m_error.Clear();
222 
223             // Call the pure virtual function to update the value
224 
225             bool need_compare_checksums = false;
226             llvm::SmallVector<uint8_t, 16> old_checksum;
227 
228             if (!first_update && CanProvideValue())
229             {
230                 need_compare_checksums = true;
231                 old_checksum.resize(m_value_checksum.size());
232                 std::copy(m_value_checksum.begin(), m_value_checksum.end(), old_checksum.begin());
233             }
234 
235             bool success = UpdateValue ();
236 
237             SetValueIsValid (success);
238 
239             if (success)
240             {
241                 const uint64_t max_checksum_size = 128;
242                 m_data.Checksum(m_value_checksum,
243                                 max_checksum_size);
244             }
245             else
246             {
247                 need_compare_checksums = false;
248                 m_value_checksum.clear();
249             }
250 
251             assert (!need_compare_checksums || (!old_checksum.empty() && !m_value_checksum.empty()));
252 
253             if (first_update)
254                 SetValueDidChange (false);
255             else if (!m_value_did_change && success == false)
256             {
257                 // The value wasn't gotten successfully, so we mark this
258                 // as changed if the value used to be valid and now isn't
259                 SetValueDidChange (value_was_valid);
260             }
261             else if (need_compare_checksums)
262             {
263                 SetValueDidChange(memcmp(&old_checksum[0], &m_value_checksum[0], m_value_checksum.size()));
264             }
265 
266         }
267         else
268         {
269             m_error.SetErrorString("out of scope");
270         }
271     }
272     return m_error.Success();
273 }
274 
275 bool
276 ValueObject::UpdateFormatsIfNeeded()
277 {
278     Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_DATAFORMATTERS));
279     if (log)
280         log->Printf("[%s %p] checking for FormatManager revisions. ValueObject rev: %d - Global rev: %d",
281                     GetName().GetCString(), static_cast<void*>(this),
282                     m_last_format_mgr_revision,
283                     DataVisualization::GetCurrentRevision());
284 
285     bool any_change = false;
286 
287     if ( (m_last_format_mgr_revision != DataVisualization::GetCurrentRevision()))
288     {
289         m_last_format_mgr_revision = DataVisualization::GetCurrentRevision();
290         any_change = true;
291 
292         SetValueFormat(DataVisualization::GetFormat (*this, eNoDynamicValues));
293         SetSummaryFormat(DataVisualization::GetSummaryFormat (*this, GetDynamicValueType()));
294 #ifndef LLDB_DISABLE_PYTHON
295         SetSyntheticChildren(DataVisualization::GetSyntheticChildren (*this, GetDynamicValueType()));
296 #endif
297         SetValidator(DataVisualization::GetValidator(*this, GetDynamicValueType()));
298     }
299 
300     return any_change;
301 }
302 
303 void
304 ValueObject::SetNeedsUpdate ()
305 {
306     m_update_point.SetNeedsUpdate();
307     // We have to clear the value string here so ConstResult children will notice if their values are
308     // changed by hand (i.e. with SetValueAsCString).
309     ClearUserVisibleData(eClearUserVisibleDataItemsValue);
310 }
311 
312 void
313 ValueObject::ClearDynamicTypeInformation ()
314 {
315     m_children_count_valid = false;
316     m_did_calculate_complete_objc_class_type = false;
317     m_last_format_mgr_revision = 0;
318     m_override_type = CompilerType();
319     SetValueFormat(lldb::TypeFormatImplSP());
320     SetSummaryFormat(lldb::TypeSummaryImplSP());
321     SetSyntheticChildren(lldb::SyntheticChildrenSP());
322 }
323 
324 CompilerType
325 ValueObject::MaybeCalculateCompleteType ()
326 {
327     CompilerType compiler_type(GetCompilerTypeImpl());
328 
329     if (m_did_calculate_complete_objc_class_type)
330     {
331         if (m_override_type.IsValid())
332             return m_override_type;
333         else
334             return compiler_type;
335     }
336 
337     CompilerType class_type;
338     bool is_pointer_type = false;
339 
340     if (ClangASTContext::IsObjCObjectPointerType(compiler_type, &class_type))
341     {
342         is_pointer_type = true;
343     }
344     else if (ClangASTContext::IsObjCObjectOrInterfaceType(compiler_type))
345     {
346         class_type = compiler_type;
347     }
348     else
349     {
350         return compiler_type;
351     }
352 
353     m_did_calculate_complete_objc_class_type = true;
354 
355     if (class_type)
356     {
357         ConstString class_name (class_type.GetConstTypeName());
358 
359         if (class_name)
360         {
361             ProcessSP process_sp(GetUpdatePoint().GetExecutionContextRef().GetProcessSP());
362 
363             if (process_sp)
364             {
365                 ObjCLanguageRuntime *objc_language_runtime(process_sp->GetObjCLanguageRuntime());
366 
367                 if (objc_language_runtime)
368                 {
369                     TypeSP complete_objc_class_type_sp = objc_language_runtime->LookupInCompleteClassCache(class_name);
370 
371                     if (complete_objc_class_type_sp)
372                     {
373                         CompilerType complete_class(complete_objc_class_type_sp->GetFullCompilerType ());
374 
375                         if (complete_class.GetCompleteType())
376                         {
377                             if (is_pointer_type)
378                             {
379                                 m_override_type = complete_class.GetPointerType();
380                             }
381                             else
382                             {
383                                 m_override_type = complete_class;
384                             }
385 
386                             if (m_override_type.IsValid())
387                                 return m_override_type;
388                         }
389                     }
390                 }
391             }
392         }
393     }
394     return compiler_type;
395 }
396 
397 CompilerType
398 ValueObject::GetCompilerType ()
399 {
400     return MaybeCalculateCompleteType();
401 }
402 
403 TypeImpl
404 ValueObject::GetTypeImpl ()
405 {
406     return TypeImpl(GetCompilerType());
407 }
408 
409 DataExtractor &
410 ValueObject::GetDataExtractor ()
411 {
412     UpdateValueIfNeeded(false);
413     return m_data;
414 }
415 
416 const Error &
417 ValueObject::GetError()
418 {
419     UpdateValueIfNeeded(false);
420     return m_error;
421 }
422 
423 const ConstString &
424 ValueObject::GetName() const
425 {
426     return m_name;
427 }
428 
429 const char *
430 ValueObject::GetLocationAsCString ()
431 {
432     return GetLocationAsCStringImpl(m_value,
433                                     m_data);
434 }
435 
436 const char *
437 ValueObject::GetLocationAsCStringImpl (const Value& value,
438                                        const DataExtractor& data)
439 {
440     if (UpdateValueIfNeeded(false))
441     {
442         if (m_location_str.empty())
443         {
444             StreamString sstr;
445 
446             Value::ValueType value_type = value.GetValueType();
447 
448             switch (value_type)
449             {
450             case Value::eValueTypeScalar:
451             case Value::eValueTypeVector:
452                 if (value.GetContextType() == Value::eContextTypeRegisterInfo)
453                 {
454                     RegisterInfo *reg_info = value.GetRegisterInfo();
455                     if (reg_info)
456                     {
457                         if (reg_info->name)
458                             m_location_str = reg_info->name;
459                         else if (reg_info->alt_name)
460                             m_location_str = reg_info->alt_name;
461                         if (m_location_str.empty())
462                             m_location_str = (reg_info->encoding == lldb::eEncodingVector) ? "vector" : "scalar";
463                     }
464                 }
465                 if (m_location_str.empty())
466                     m_location_str = (value_type == Value::eValueTypeVector) ? "vector" : "scalar";
467                 break;
468 
469             case Value::eValueTypeLoadAddress:
470             case Value::eValueTypeFileAddress:
471             case Value::eValueTypeHostAddress:
472                 {
473                     uint32_t addr_nibble_size = data.GetAddressByteSize() * 2;
474                     sstr.Printf("0x%*.*llx", addr_nibble_size, addr_nibble_size, value.GetScalar().ULongLong(LLDB_INVALID_ADDRESS));
475                     m_location_str.swap(sstr.GetString());
476                 }
477                 break;
478             }
479         }
480     }
481     return m_location_str.c_str();
482 }
483 
484 Value &
485 ValueObject::GetValue()
486 {
487     return m_value;
488 }
489 
490 const Value &
491 ValueObject::GetValue() const
492 {
493     return m_value;
494 }
495 
496 bool
497 ValueObject::ResolveValue (Scalar &scalar)
498 {
499     if (UpdateValueIfNeeded(false)) // make sure that you are up to date before returning anything
500     {
501         ExecutionContext exe_ctx (GetExecutionContextRef());
502         Value tmp_value(m_value);
503         scalar = tmp_value.ResolveValue(&exe_ctx);
504         if (scalar.IsValid())
505         {
506             const uint32_t bitfield_bit_size = GetBitfieldBitSize();
507             if (bitfield_bit_size)
508                 return scalar.ExtractBitfield (bitfield_bit_size, GetBitfieldBitOffset());
509             return true;
510         }
511     }
512     return false;
513 }
514 
515 bool
516 ValueObject::GetValueIsValid () const
517 {
518     return m_value_is_valid;
519 }
520 
521 
522 void
523 ValueObject::SetValueIsValid (bool b)
524 {
525     m_value_is_valid = b;
526 }
527 
528 bool
529 ValueObject::GetValueDidChange ()
530 {
531     return m_value_did_change;
532 }
533 
534 void
535 ValueObject::SetValueDidChange (bool value_changed)
536 {
537     m_value_did_change = value_changed;
538 }
539 
540 ValueObjectSP
541 ValueObject::GetChildAtIndex (size_t idx, bool can_create)
542 {
543     ValueObjectSP child_sp;
544     // We may need to update our value if we are dynamic
545     if (IsPossibleDynamicType ())
546         UpdateValueIfNeeded(false);
547     if (idx < GetNumChildren())
548     {
549         // Check if we have already made the child value object?
550         if (can_create && !m_children.HasChildAtIndex(idx))
551         {
552             // No we haven't created the child at this index, so lets have our
553             // subclass do it and cache the result for quick future access.
554             m_children.SetChildAtIndex(idx,CreateChildAtIndex (idx, false, 0));
555         }
556 
557         ValueObject* child = m_children.GetChildAtIndex(idx);
558         if (child != NULL)
559             return child->GetSP();
560     }
561     return child_sp;
562 }
563 
564 ValueObjectSP
565 ValueObject::GetChildAtIndexPath (const std::initializer_list<size_t>& idxs,
566                                   size_t* index_of_error)
567 {
568     if (idxs.size() == 0)
569         return GetSP();
570     ValueObjectSP root(GetSP());
571     for (size_t idx : idxs)
572     {
573         root = root->GetChildAtIndex(idx, true);
574         if (!root)
575         {
576             if (index_of_error)
577                 *index_of_error = idx;
578             return root;
579         }
580     }
581     return root;
582 }
583 
584 ValueObjectSP
585 ValueObject::GetChildAtIndexPath (const std::initializer_list< std::pair<size_t, bool> >& idxs,
586                                   size_t* index_of_error)
587 {
588     if (idxs.size() == 0)
589         return GetSP();
590     ValueObjectSP root(GetSP());
591     for (std::pair<size_t, bool> idx : idxs)
592     {
593         root = root->GetChildAtIndex(idx.first, idx.second);
594         if (!root)
595         {
596             if (index_of_error)
597                 *index_of_error = idx.first;
598             return root;
599         }
600     }
601     return root;
602 }
603 
604 lldb::ValueObjectSP
605 ValueObject::GetChildAtIndexPath (const std::vector<size_t> &idxs,
606                                   size_t* index_of_error)
607 {
608     if (idxs.size() == 0)
609         return GetSP();
610     ValueObjectSP root(GetSP());
611     for (size_t idx : idxs)
612     {
613         root = root->GetChildAtIndex(idx, true);
614         if (!root)
615         {
616             if (index_of_error)
617                 *index_of_error = idx;
618             return root;
619         }
620     }
621     return root;
622 }
623 
624 lldb::ValueObjectSP
625 ValueObject::GetChildAtIndexPath (const std::vector< std::pair<size_t, bool> > &idxs,
626                                   size_t* index_of_error)
627 {
628     if (idxs.size() == 0)
629         return GetSP();
630     ValueObjectSP root(GetSP());
631     for (std::pair<size_t, bool> idx : idxs)
632     {
633         root = root->GetChildAtIndex(idx.first, idx.second);
634         if (!root)
635         {
636             if (index_of_error)
637                 *index_of_error = idx.first;
638             return root;
639         }
640     }
641     return root;
642 }
643 
644 lldb::ValueObjectSP
645 ValueObject::GetChildAtNamePath (const std::initializer_list<ConstString> &names,
646                                  ConstString* name_of_error)
647 {
648     if (names.size() == 0)
649         return GetSP();
650     ValueObjectSP root(GetSP());
651     for (ConstString name : names)
652     {
653         root = root->GetChildMemberWithName(name, true);
654         if (!root)
655         {
656             if (name_of_error)
657                 *name_of_error = name;
658             return root;
659         }
660     }
661     return root;
662 }
663 
664 lldb::ValueObjectSP
665 ValueObject::GetChildAtNamePath (const std::vector<ConstString> &names,
666                                  ConstString* name_of_error)
667 {
668     if (names.size() == 0)
669         return GetSP();
670     ValueObjectSP root(GetSP());
671     for (ConstString name : names)
672     {
673         root = root->GetChildMemberWithName(name, true);
674         if (!root)
675         {
676             if (name_of_error)
677                 *name_of_error = name;
678             return root;
679         }
680     }
681     return root;
682 }
683 
684 lldb::ValueObjectSP
685 ValueObject::GetChildAtNamePath (const std::initializer_list< std::pair<ConstString, bool> > &names,
686                                  ConstString* name_of_error)
687 {
688     if (names.size() == 0)
689         return GetSP();
690     ValueObjectSP root(GetSP());
691     for (std::pair<ConstString, bool> name : names)
692     {
693         root = root->GetChildMemberWithName(name.first, name.second);
694         if (!root)
695         {
696             if (name_of_error)
697                 *name_of_error = name.first;
698             return root;
699         }
700     }
701     return root;
702 }
703 
704 lldb::ValueObjectSP
705 ValueObject::GetChildAtNamePath (const std::vector< std::pair<ConstString, bool> > &names,
706                                  ConstString* name_of_error)
707 {
708     if (names.size() == 0)
709         return GetSP();
710         ValueObjectSP root(GetSP());
711         for (std::pair<ConstString, bool> name : names)
712         {
713             root = root->GetChildMemberWithName(name.first, name.second);
714             if (!root)
715             {
716                 if (name_of_error)
717                     *name_of_error = name.first;
718                     return root;
719             }
720         }
721     return root;
722 }
723 
724 size_t
725 ValueObject::GetIndexOfChildWithName (const ConstString &name)
726 {
727     bool omit_empty_base_classes = true;
728     return GetCompilerType().GetIndexOfChildWithName (name.GetCString(), omit_empty_base_classes);
729 }
730 
731 ValueObjectSP
732 ValueObject::GetChildMemberWithName (const ConstString &name, bool can_create)
733 {
734     // when getting a child by name, it could be buried inside some base
735     // classes (which really aren't part of the expression path), so we
736     // need a vector of indexes that can get us down to the correct child
737     ValueObjectSP child_sp;
738 
739     // We may need to update our value if we are dynamic
740     if (IsPossibleDynamicType ())
741         UpdateValueIfNeeded(false);
742 
743     std::vector<uint32_t> child_indexes;
744     bool omit_empty_base_classes = true;
745     const size_t num_child_indexes =  GetCompilerType().GetIndexOfChildMemberWithName (name.GetCString(),
746                                                                                     omit_empty_base_classes,
747                                                                                     child_indexes);
748     if (num_child_indexes > 0)
749     {
750         std::vector<uint32_t>::const_iterator pos = child_indexes.begin ();
751         std::vector<uint32_t>::const_iterator end = child_indexes.end ();
752 
753         child_sp = GetChildAtIndex(*pos, can_create);
754         for (++pos; pos != end; ++pos)
755         {
756             if (child_sp)
757             {
758                 ValueObjectSP new_child_sp(child_sp->GetChildAtIndex (*pos, can_create));
759                 child_sp = new_child_sp;
760             }
761             else
762             {
763                 child_sp.reset();
764             }
765 
766         }
767     }
768     return child_sp;
769 }
770 
771 
772 size_t
773 ValueObject::GetNumChildren ()
774 {
775     UpdateValueIfNeeded();
776     if (!m_children_count_valid)
777     {
778         SetNumChildren (CalculateNumChildren());
779     }
780     return m_children.GetChildrenCount();
781 }
782 
783 bool
784 ValueObject::MightHaveChildren()
785 {
786     bool has_children = false;
787     const uint32_t type_info = GetTypeInfo();
788     if (type_info)
789     {
790         if (type_info & (eTypeHasChildren |
791                          eTypeIsPointer |
792                          eTypeIsReference))
793             has_children = true;
794     }
795     else
796     {
797         has_children = GetNumChildren () > 0;
798     }
799     return has_children;
800 }
801 
802 // Should only be called by ValueObject::GetNumChildren()
803 void
804 ValueObject::SetNumChildren (size_t num_children)
805 {
806     m_children_count_valid = true;
807     m_children.SetChildrenCount(num_children);
808 }
809 
810 void
811 ValueObject::SetName (const ConstString &name)
812 {
813     m_name = name;
814 }
815 
816 ValueObject *
817 ValueObject::CreateChildAtIndex (size_t idx, bool synthetic_array_member, int32_t synthetic_index)
818 {
819     ValueObject *valobj = NULL;
820 
821     bool omit_empty_base_classes = true;
822     bool ignore_array_bounds = synthetic_array_member;
823     std::string child_name_str;
824     uint32_t child_byte_size = 0;
825     int32_t child_byte_offset = 0;
826     uint32_t child_bitfield_bit_size = 0;
827     uint32_t child_bitfield_bit_offset = 0;
828     bool child_is_base_class = false;
829     bool child_is_deref_of_parent = false;
830 
831     const bool transparent_pointers = synthetic_array_member == false;
832     CompilerType child_compiler_type;
833 
834     ExecutionContext exe_ctx (GetExecutionContextRef());
835 
836     child_compiler_type = GetCompilerType().GetChildCompilerTypeAtIndex (&exe_ctx,
837                                                                       idx,
838                                                                       transparent_pointers,
839                                                                       omit_empty_base_classes,
840                                                                       ignore_array_bounds,
841                                                                       child_name_str,
842                                                                       child_byte_size,
843                                                                       child_byte_offset,
844                                                                       child_bitfield_bit_size,
845                                                                       child_bitfield_bit_offset,
846                                                                       child_is_base_class,
847                                                                       child_is_deref_of_parent,
848                                                                       this);
849     if (child_compiler_type)
850     {
851         if (synthetic_index)
852             child_byte_offset += child_byte_size * synthetic_index;
853 
854         ConstString child_name;
855         if (!child_name_str.empty())
856             child_name.SetCString (child_name_str.c_str());
857 
858         valobj = new ValueObjectChild (*this,
859                                        child_compiler_type,
860                                        child_name,
861                                        child_byte_size,
862                                        child_byte_offset,
863                                        child_bitfield_bit_size,
864                                        child_bitfield_bit_offset,
865                                        child_is_base_class,
866                                        child_is_deref_of_parent,
867                                        eAddressTypeInvalid);
868         //if (valobj)
869         //    valobj->SetAddressTypeOfChildren(eAddressTypeInvalid);
870    }
871 
872     return valobj;
873 }
874 
875 bool
876 ValueObject::GetSummaryAsCString (TypeSummaryImpl* summary_ptr,
877                                   std::string& destination,
878                                   lldb::LanguageType lang)
879 {
880     return GetSummaryAsCString(summary_ptr, destination, TypeSummaryOptions().SetLanguage(lang));
881 }
882 
883 bool
884 ValueObject::GetSummaryAsCString (TypeSummaryImpl* summary_ptr,
885                                   std::string& destination,
886                                   const TypeSummaryOptions& options)
887 {
888     destination.clear();
889 
890     // ideally we would like to bail out if passing NULL, but if we do so
891     // we end up not providing the summary for function pointers anymore
892     if (/*summary_ptr == NULL ||*/ m_is_getting_summary)
893         return false;
894 
895     m_is_getting_summary = true;
896 
897     TypeSummaryOptions actual_options(options);
898 
899     if (actual_options.GetLanguage() == lldb::eLanguageTypeUnknown)
900         actual_options.SetLanguage(GetPreferredDisplayLanguage());
901 
902     // this is a hot path in code and we prefer to avoid setting this string all too often also clearing out other
903     // information that we might care to see in a crash log. might be useful in very specific situations though.
904     /*Host::SetCrashDescriptionWithFormat("Trying to fetch a summary for %s %s. Summary provider's description is %s",
905      GetTypeName().GetCString(),
906      GetName().GetCString(),
907      summary_ptr->GetDescription().c_str());*/
908 
909     if (UpdateValueIfNeeded (false) && summary_ptr)
910     {
911         if (HasSyntheticValue())
912             m_synthetic_value->UpdateValueIfNeeded(); // the summary might depend on the synthetic children being up-to-date (e.g. ${svar%#})
913         summary_ptr->FormatObject(this, destination, actual_options);
914     }
915     m_is_getting_summary = false;
916     return !destination.empty();
917 }
918 
919 const char *
920 ValueObject::GetSummaryAsCString (lldb::LanguageType lang)
921 {
922     if (UpdateValueIfNeeded(true) && m_summary_str.empty())
923     {
924         TypeSummaryOptions summary_options;
925         summary_options.SetLanguage(lang);
926         GetSummaryAsCString(GetSummaryFormat().get(),
927                             m_summary_str,
928                             summary_options);
929     }
930     if (m_summary_str.empty())
931         return NULL;
932     return m_summary_str.c_str();
933 }
934 
935 bool
936 ValueObject::GetSummaryAsCString (std::string& destination,
937                                   const TypeSummaryOptions& options)
938 {
939     return GetSummaryAsCString(GetSummaryFormat().get(),
940                                destination,
941                                options);
942 }
943 
944 bool
945 ValueObject::IsCStringContainer(bool check_pointer)
946 {
947     CompilerType pointee_or_element_compiler_type;
948     const Flags type_flags (GetTypeInfo (&pointee_or_element_compiler_type));
949     bool is_char_arr_ptr (type_flags.AnySet (eTypeIsArray | eTypeIsPointer) &&
950                           pointee_or_element_compiler_type.IsCharType ());
951     if (!is_char_arr_ptr)
952         return false;
953     if (!check_pointer)
954         return true;
955     if (type_flags.Test(eTypeIsArray))
956         return true;
957     addr_t cstr_address = LLDB_INVALID_ADDRESS;
958     AddressType cstr_address_type = eAddressTypeInvalid;
959     cstr_address = GetAddressOf (true, &cstr_address_type);
960     return (cstr_address != LLDB_INVALID_ADDRESS);
961 }
962 
963 size_t
964 ValueObject::GetPointeeData (DataExtractor& data,
965                              uint32_t item_idx,
966                              uint32_t item_count)
967 {
968     CompilerType pointee_or_element_compiler_type;
969     const uint32_t type_info = GetTypeInfo (&pointee_or_element_compiler_type);
970     const bool is_pointer_type = type_info & eTypeIsPointer;
971     const bool is_array_type = type_info & eTypeIsArray;
972     if (!(is_pointer_type || is_array_type))
973         return 0;
974 
975     if (item_count == 0)
976         return 0;
977 
978     ExecutionContext exe_ctx (GetExecutionContextRef());
979 
980     const uint64_t item_type_size = pointee_or_element_compiler_type.GetByteSize(exe_ctx.GetBestExecutionContextScope());
981     const uint64_t bytes = item_count * item_type_size;
982     const uint64_t offset = item_idx * item_type_size;
983 
984     if (item_idx == 0 && item_count == 1) // simply a deref
985     {
986         if (is_pointer_type)
987         {
988             Error error;
989             ValueObjectSP pointee_sp = Dereference(error);
990             if (error.Fail() || pointee_sp.get() == NULL)
991                 return 0;
992             return pointee_sp->GetData(data, error);
993         }
994         else
995         {
996             ValueObjectSP child_sp = GetChildAtIndex(0, true);
997             if (child_sp.get() == NULL)
998                 return 0;
999             Error error;
1000             return child_sp->GetData(data, error);
1001         }
1002         return true;
1003     }
1004     else /* (items > 1) */
1005     {
1006         Error error;
1007         lldb_private::DataBufferHeap* heap_buf_ptr = NULL;
1008         lldb::DataBufferSP data_sp(heap_buf_ptr = new lldb_private::DataBufferHeap());
1009 
1010         AddressType addr_type;
1011         lldb::addr_t addr = is_pointer_type ? GetPointerValue(&addr_type) : GetAddressOf(true, &addr_type);
1012 
1013         switch (addr_type)
1014         {
1015             case eAddressTypeFile:
1016                 {
1017                     ModuleSP module_sp (GetModule());
1018                     if (module_sp)
1019                     {
1020                         addr = addr + offset;
1021                         Address so_addr;
1022                         module_sp->ResolveFileAddress(addr, so_addr);
1023                         ExecutionContext exe_ctx (GetExecutionContextRef());
1024                         Target* target = exe_ctx.GetTargetPtr();
1025                         if (target)
1026                         {
1027                             heap_buf_ptr->SetByteSize(bytes);
1028                             size_t bytes_read = target->ReadMemory(so_addr, false, heap_buf_ptr->GetBytes(), bytes, error);
1029                             if (error.Success())
1030                             {
1031                                 data.SetData(data_sp);
1032                                 return bytes_read;
1033                             }
1034                         }
1035                     }
1036                 }
1037                 break;
1038             case eAddressTypeLoad:
1039                 {
1040                     ExecutionContext exe_ctx (GetExecutionContextRef());
1041                     Process *process = exe_ctx.GetProcessPtr();
1042                     if (process)
1043                     {
1044                         heap_buf_ptr->SetByteSize(bytes);
1045                         size_t bytes_read = process->ReadMemory(addr + offset, heap_buf_ptr->GetBytes(), bytes, error);
1046                         if (error.Success() || bytes_read > 0)
1047                         {
1048                             data.SetData(data_sp);
1049                             return bytes_read;
1050                         }
1051                     }
1052                 }
1053                 break;
1054             case eAddressTypeHost:
1055                 {
1056                     const uint64_t max_bytes = GetCompilerType().GetByteSize(exe_ctx.GetBestExecutionContextScope());
1057                     if (max_bytes > offset)
1058                     {
1059                         size_t bytes_read = std::min<uint64_t>(max_bytes - offset, bytes);
1060                         addr = m_value.GetScalar().ULongLong(LLDB_INVALID_ADDRESS);
1061                         if (addr == LLDB_INVALID_ADDRESS)
1062                             break;
1063                         heap_buf_ptr->CopyData((uint8_t*)(addr + offset), bytes_read);
1064                         data.SetData(data_sp);
1065                         return bytes_read;
1066                     }
1067                 }
1068                 break;
1069             case eAddressTypeInvalid:
1070                 break;
1071         }
1072     }
1073     return 0;
1074 }
1075 
1076 uint64_t
1077 ValueObject::GetData (DataExtractor& data, Error &error)
1078 {
1079     UpdateValueIfNeeded(false);
1080     ExecutionContext exe_ctx (GetExecutionContextRef());
1081     error = m_value.GetValueAsData(&exe_ctx, data, 0, GetModule().get());
1082     if (error.Fail())
1083     {
1084         if (m_data.GetByteSize())
1085         {
1086             data = m_data;
1087             return data.GetByteSize();
1088         }
1089         else
1090         {
1091             return 0;
1092         }
1093     }
1094     data.SetAddressByteSize(m_data.GetAddressByteSize());
1095     data.SetByteOrder(m_data.GetByteOrder());
1096     return data.GetByteSize();
1097 }
1098 
1099 bool
1100 ValueObject::SetData (DataExtractor &data, Error &error)
1101 {
1102     error.Clear();
1103     // Make sure our value is up to date first so that our location and location
1104     // type is valid.
1105     if (!UpdateValueIfNeeded(false))
1106     {
1107         error.SetErrorString("unable to read value");
1108         return false;
1109     }
1110 
1111     uint64_t count = 0;
1112     const Encoding encoding = GetCompilerType().GetEncoding(count);
1113 
1114     const size_t byte_size = GetByteSize();
1115 
1116     Value::ValueType value_type = m_value.GetValueType();
1117 
1118     switch (value_type)
1119     {
1120     case Value::eValueTypeScalar:
1121         {
1122             Error set_error = m_value.GetScalar().SetValueFromData(data, encoding, byte_size);
1123 
1124             if (!set_error.Success())
1125             {
1126                 error.SetErrorStringWithFormat("unable to set scalar value: %s", set_error.AsCString());
1127                 return false;
1128             }
1129         }
1130         break;
1131     case Value::eValueTypeLoadAddress:
1132         {
1133             // If it is a load address, then the scalar value is the storage location
1134             // of the data, and we have to shove this value down to that load location.
1135             ExecutionContext exe_ctx (GetExecutionContextRef());
1136             Process *process = exe_ctx.GetProcessPtr();
1137             if (process)
1138             {
1139                 addr_t target_addr = m_value.GetScalar().ULongLong(LLDB_INVALID_ADDRESS);
1140                 size_t bytes_written = process->WriteMemory(target_addr,
1141                                                             data.GetDataStart(),
1142                                                             byte_size,
1143                                                             error);
1144                 if (!error.Success())
1145                     return false;
1146                 if (bytes_written != byte_size)
1147                 {
1148                     error.SetErrorString("unable to write value to memory");
1149                     return false;
1150                 }
1151             }
1152         }
1153         break;
1154     case Value::eValueTypeHostAddress:
1155         {
1156             // If it is a host address, then we stuff the scalar as a DataBuffer into the Value's data.
1157             DataBufferSP buffer_sp (new DataBufferHeap(byte_size, 0));
1158             m_data.SetData(buffer_sp, 0);
1159             data.CopyByteOrderedData (0,
1160                                       byte_size,
1161                                       const_cast<uint8_t *>(m_data.GetDataStart()),
1162                                       byte_size,
1163                                       m_data.GetByteOrder());
1164             m_value.GetScalar() = (uintptr_t)m_data.GetDataStart();
1165         }
1166         break;
1167     case Value::eValueTypeFileAddress:
1168     case Value::eValueTypeVector:
1169         break;
1170     }
1171 
1172     // If we have reached this point, then we have successfully changed the value.
1173     SetNeedsUpdate();
1174     return true;
1175 }
1176 
1177 // will compute strlen(str), but without consuming more than
1178 // maxlen bytes out of str (this serves the purpose of reading
1179 // chunks of a string without having to worry about
1180 // missing NULL terminators in the chunk)
1181 // of course, if strlen(str) > maxlen, the function will return
1182 // maxlen_value (which should be != maxlen, because that allows you
1183 // to know whether strlen(str) == maxlen or strlen(str) > maxlen)
1184 static uint32_t
1185 strlen_or_inf (const char* str,
1186                uint32_t maxlen,
1187                uint32_t maxlen_value)
1188 {
1189     uint32_t len = 0;
1190     if (str)
1191     {
1192         while(*str)
1193         {
1194             len++;str++;
1195             if (len >= maxlen)
1196                 return maxlen_value;
1197         }
1198     }
1199     return len;
1200 }
1201 
1202 static bool
1203 CopyStringDataToBufferSP(const StreamString& source,
1204                          lldb::DataBufferSP& destination)
1205 {
1206     destination.reset(new DataBufferHeap(source.GetSize()+1,0));
1207     memcpy(destination->GetBytes(), source.GetString().c_str(), source.GetSize());
1208     return true;
1209 }
1210 
1211 size_t
1212 ValueObject::ReadPointedString (lldb::DataBufferSP& buffer_sp,
1213                                 Error& error,
1214                                 uint32_t max_length,
1215                                 bool honor_array,
1216                                 Format item_format)
1217 {
1218     StreamString s;
1219     ExecutionContext exe_ctx (GetExecutionContextRef());
1220     Target* target = exe_ctx.GetTargetPtr();
1221 
1222     if (!target)
1223     {
1224         s << "<no target to read from>";
1225         error.SetErrorString("no target to read from");
1226         CopyStringDataToBufferSP(s, buffer_sp);
1227         return 0;
1228     }
1229 
1230     if (max_length == 0)
1231         max_length = target->GetMaximumSizeOfStringSummary();
1232 
1233     size_t bytes_read = 0;
1234     size_t total_bytes_read = 0;
1235 
1236     CompilerType compiler_type = GetCompilerType();
1237     CompilerType elem_or_pointee_compiler_type;
1238     const Flags type_flags (GetTypeInfo (&elem_or_pointee_compiler_type));
1239     if (type_flags.AnySet (eTypeIsArray | eTypeIsPointer) &&
1240         elem_or_pointee_compiler_type.IsCharType ())
1241     {
1242         addr_t cstr_address = LLDB_INVALID_ADDRESS;
1243         AddressType cstr_address_type = eAddressTypeInvalid;
1244 
1245         size_t cstr_len = 0;
1246         bool capped_data = false;
1247         if (type_flags.Test (eTypeIsArray))
1248         {
1249             // We have an array
1250             uint64_t array_size = 0;
1251             if (compiler_type.IsArrayType(NULL, &array_size, NULL))
1252             {
1253                 cstr_len = array_size;
1254                 if (cstr_len > max_length)
1255                 {
1256                     capped_data = true;
1257                     cstr_len = max_length;
1258                 }
1259             }
1260             cstr_address = GetAddressOf (true, &cstr_address_type);
1261         }
1262         else
1263         {
1264             // We have a pointer
1265             cstr_address = GetPointerValue (&cstr_address_type);
1266         }
1267 
1268         if (cstr_address == 0 || cstr_address == LLDB_INVALID_ADDRESS)
1269         {
1270             s << "<invalid address>";
1271             error.SetErrorString("invalid address");
1272             CopyStringDataToBufferSP(s, buffer_sp);
1273             return 0;
1274         }
1275 
1276         Address cstr_so_addr (cstr_address);
1277         DataExtractor data;
1278         if (cstr_len > 0 && honor_array)
1279         {
1280             // I am using GetPointeeData() here to abstract the fact that some ValueObjects are actually frozen pointers in the host
1281             // but the pointed-to data lives in the debuggee, and GetPointeeData() automatically takes care of this
1282             GetPointeeData(data, 0, cstr_len);
1283 
1284             if ((bytes_read = data.GetByteSize()) > 0)
1285             {
1286                 total_bytes_read = bytes_read;
1287                 for (size_t offset = 0; offset < bytes_read; offset++)
1288                     s.Printf("%c", *data.PeekData(offset, 1));
1289                 if (capped_data)
1290                     s << "...";
1291             }
1292         }
1293         else
1294         {
1295             cstr_len = max_length;
1296             const size_t k_max_buf_size = 64;
1297 
1298             size_t offset = 0;
1299 
1300             int cstr_len_displayed = -1;
1301             bool capped_cstr = false;
1302             // I am using GetPointeeData() here to abstract the fact that some ValueObjects are actually frozen pointers in the host
1303             // but the pointed-to data lives in the debuggee, and GetPointeeData() automatically takes care of this
1304             while ((bytes_read = GetPointeeData(data, offset, k_max_buf_size)) > 0)
1305             {
1306                 total_bytes_read += bytes_read;
1307                 const char *cstr = data.PeekCStr(0);
1308                 size_t len = strlen_or_inf (cstr, k_max_buf_size, k_max_buf_size+1);
1309                 if (len > k_max_buf_size)
1310                     len = k_max_buf_size;
1311 
1312                 if (cstr_len_displayed < 0)
1313                     cstr_len_displayed = len;
1314 
1315                 if (len == 0)
1316                     break;
1317                 cstr_len_displayed += len;
1318                 if (len > bytes_read)
1319                     len = bytes_read;
1320                 if (len > cstr_len)
1321                     len = cstr_len;
1322 
1323                 for (size_t offset = 0; offset < bytes_read; offset++)
1324                     s.Printf("%c", *data.PeekData(offset, 1));
1325 
1326                 if (len < k_max_buf_size)
1327                     break;
1328 
1329                 if (len >= cstr_len)
1330                 {
1331                     capped_cstr = true;
1332                     break;
1333                 }
1334 
1335                 cstr_len -= len;
1336                 offset += len;
1337             }
1338 
1339             if (cstr_len_displayed >= 0)
1340             {
1341                 if (capped_cstr)
1342                     s << "...";
1343             }
1344         }
1345     }
1346     else
1347     {
1348         error.SetErrorString("not a string object");
1349         s << "<not a string object>";
1350     }
1351     CopyStringDataToBufferSP(s, buffer_sp);
1352     return total_bytes_read;
1353 }
1354 
1355 std::pair<TypeValidatorResult, std::string>
1356 ValueObject::GetValidationStatus ()
1357 {
1358     if (!UpdateValueIfNeeded(true))
1359         return {TypeValidatorResult::Success,""}; // not the validator's job to discuss update problems
1360 
1361     if (m_validation_result.hasValue())
1362         return m_validation_result.getValue();
1363 
1364     if (!m_type_validator_sp)
1365         return {TypeValidatorResult::Success,""}; // no validator no failure
1366 
1367     auto outcome = m_type_validator_sp->FormatObject(this);
1368 
1369     return (m_validation_result = {outcome.m_result,outcome.m_message}).getValue();
1370 }
1371 
1372 const char *
1373 ValueObject::GetObjectDescription ()
1374 {
1375 
1376     if (!UpdateValueIfNeeded (true))
1377         return NULL;
1378 
1379     if (!m_object_desc_str.empty())
1380         return m_object_desc_str.c_str();
1381 
1382     ExecutionContext exe_ctx (GetExecutionContextRef());
1383     Process *process = exe_ctx.GetProcessPtr();
1384     if (process == NULL)
1385         return NULL;
1386 
1387     StreamString s;
1388 
1389     LanguageType language = GetObjectRuntimeLanguage();
1390     LanguageRuntime *runtime = process->GetLanguageRuntime(language);
1391 
1392     if (runtime == NULL)
1393     {
1394         // Aw, hell, if the things a pointer, or even just an integer, let's try ObjC anyway...
1395         CompilerType compiler_type = GetCompilerType();
1396         if (compiler_type)
1397         {
1398             bool is_signed;
1399             if (compiler_type.IsIntegerType (is_signed) || compiler_type.IsPointerType ())
1400             {
1401                 runtime = process->GetLanguageRuntime(eLanguageTypeObjC);
1402             }
1403         }
1404     }
1405 
1406     if (runtime && runtime->GetObjectDescription(s, *this))
1407     {
1408         m_object_desc_str.append (s.GetData());
1409     }
1410 
1411     if (m_object_desc_str.empty())
1412         return NULL;
1413     else
1414         return m_object_desc_str.c_str();
1415 }
1416 
1417 bool
1418 ValueObject::GetValueAsCString (const lldb_private::TypeFormatImpl& format,
1419                                 std::string& destination)
1420 {
1421     if (UpdateValueIfNeeded(false))
1422         return format.FormatObject(this,destination);
1423     else
1424         return false;
1425 }
1426 
1427 bool
1428 ValueObject::GetValueAsCString (lldb::Format format,
1429                                 std::string& destination)
1430 {
1431     return GetValueAsCString(TypeFormatImpl_Format(format),destination);
1432 }
1433 
1434 const char *
1435 ValueObject::GetValueAsCString ()
1436 {
1437     if (UpdateValueIfNeeded(true))
1438     {
1439         lldb::TypeFormatImplSP format_sp;
1440         lldb::Format my_format = GetFormat();
1441         if (my_format == lldb::eFormatDefault)
1442         {
1443             if (m_type_format_sp)
1444                 format_sp = m_type_format_sp;
1445             else
1446             {
1447                 if (m_is_bitfield_for_scalar)
1448                     my_format = eFormatUnsigned;
1449                 else
1450                 {
1451                     if (m_value.GetContextType() == Value::eContextTypeRegisterInfo)
1452                     {
1453                         const RegisterInfo *reg_info = m_value.GetRegisterInfo();
1454                         if (reg_info)
1455                             my_format = reg_info->format;
1456                     }
1457                     else
1458                     {
1459                         my_format = GetValue().GetCompilerType().GetFormat();
1460                     }
1461                 }
1462             }
1463         }
1464         if (my_format != m_last_format || m_value_str.empty())
1465         {
1466             m_last_format = my_format;
1467             if (!format_sp)
1468                 format_sp.reset(new TypeFormatImpl_Format(my_format));
1469             if (GetValueAsCString(*format_sp.get(), m_value_str))
1470             {
1471                 if (!m_value_did_change && m_old_value_valid)
1472                 {
1473                     // The value was gotten successfully, so we consider the
1474                     // value as changed if the value string differs
1475                     SetValueDidChange (m_old_value_str != m_value_str);
1476                 }
1477             }
1478         }
1479     }
1480     if (m_value_str.empty())
1481         return NULL;
1482     return m_value_str.c_str();
1483 }
1484 
1485 // if > 8bytes, 0 is returned. this method should mostly be used
1486 // to read address values out of pointers
1487 uint64_t
1488 ValueObject::GetValueAsUnsigned (uint64_t fail_value, bool *success)
1489 {
1490     // If our byte size is zero this is an aggregate type that has children
1491     if (CanProvideValue())
1492     {
1493         Scalar scalar;
1494         if (ResolveValue (scalar))
1495         {
1496             if (success)
1497                 *success = true;
1498             return scalar.ULongLong(fail_value);
1499         }
1500         // fallthrough, otherwise...
1501     }
1502 
1503     if (success)
1504         *success = false;
1505     return fail_value;
1506 }
1507 
1508 int64_t
1509 ValueObject::GetValueAsSigned (int64_t fail_value, bool *success)
1510 {
1511     // If our byte size is zero this is an aggregate type that has children
1512     if (CanProvideValue())
1513     {
1514         Scalar scalar;
1515         if (ResolveValue (scalar))
1516         {
1517             if (success)
1518                 *success = true;
1519             return scalar.SLongLong(fail_value);
1520         }
1521         // fallthrough, otherwise...
1522     }
1523 
1524     if (success)
1525         *success = false;
1526     return fail_value;
1527 }
1528 
1529 // if any more "special cases" are added to ValueObject::DumpPrintableRepresentation() please keep
1530 // this call up to date by returning true for your new special cases. We will eventually move
1531 // to checking this call result before trying to display special cases
1532 bool
1533 ValueObject::HasSpecialPrintableRepresentation(ValueObjectRepresentationStyle val_obj_display,
1534                                                Format custom_format)
1535 {
1536     Flags flags(GetTypeInfo());
1537     if (flags.AnySet(eTypeIsArray | eTypeIsPointer)
1538         && val_obj_display == ValueObject::eValueObjectRepresentationStyleValue)
1539     {
1540         if (IsCStringContainer(true) &&
1541             (custom_format == eFormatCString ||
1542              custom_format == eFormatCharArray ||
1543              custom_format == eFormatChar ||
1544              custom_format == eFormatVectorOfChar))
1545             return true;
1546 
1547         if (flags.Test(eTypeIsArray))
1548         {
1549             if ((custom_format == eFormatBytes) ||
1550                 (custom_format == eFormatBytesWithASCII))
1551                 return true;
1552 
1553             if ((custom_format == eFormatVectorOfChar) ||
1554                 (custom_format == eFormatVectorOfFloat32) ||
1555                 (custom_format == eFormatVectorOfFloat64) ||
1556                 (custom_format == eFormatVectorOfSInt16) ||
1557                 (custom_format == eFormatVectorOfSInt32) ||
1558                 (custom_format == eFormatVectorOfSInt64) ||
1559                 (custom_format == eFormatVectorOfSInt8) ||
1560                 (custom_format == eFormatVectorOfUInt128) ||
1561                 (custom_format == eFormatVectorOfUInt16) ||
1562                 (custom_format == eFormatVectorOfUInt32) ||
1563                 (custom_format == eFormatVectorOfUInt64) ||
1564                 (custom_format == eFormatVectorOfUInt8))
1565                 return true;
1566         }
1567     }
1568     return false;
1569 }
1570 
1571 bool
1572 ValueObject::DumpPrintableRepresentation(Stream& s,
1573                                          ValueObjectRepresentationStyle val_obj_display,
1574                                          Format custom_format,
1575                                          PrintableRepresentationSpecialCases special,
1576                                          bool do_dump_error)
1577 {
1578 
1579     Flags flags(GetTypeInfo());
1580 
1581     bool allow_special = ((special & ePrintableRepresentationSpecialCasesAllow) == ePrintableRepresentationSpecialCasesAllow);
1582     bool only_special = ((special & ePrintableRepresentationSpecialCasesOnly) == ePrintableRepresentationSpecialCasesOnly);
1583 
1584     if (allow_special)
1585     {
1586         if (flags.AnySet(eTypeIsArray | eTypeIsPointer)
1587              && val_obj_display == ValueObject::eValueObjectRepresentationStyleValue)
1588         {
1589             // when being asked to get a printable display an array or pointer type directly,
1590             // try to "do the right thing"
1591 
1592             if (IsCStringContainer(true) &&
1593                 (custom_format == eFormatCString ||
1594                  custom_format == eFormatCharArray ||
1595                  custom_format == eFormatChar ||
1596                  custom_format == eFormatVectorOfChar)) // print char[] & char* directly
1597             {
1598                 Error error;
1599                 lldb::DataBufferSP buffer_sp;
1600                 ReadPointedString(buffer_sp,
1601                                   error,
1602                                   0,
1603                                   (custom_format == eFormatVectorOfChar) ||
1604                                   (custom_format == eFormatCharArray));
1605                 lldb_private::formatters::StringPrinter::ReadBufferAndDumpToStreamOptions options(*this);
1606                 options.SetData(DataExtractor(buffer_sp, lldb::eByteOrderInvalid, 8)); // none of this matters for a string - pass some defaults
1607                 options.SetStream(&s);
1608                 options.SetPrefixToken(0);
1609                 options.SetQuote('"');
1610                 options.SetSourceSize(buffer_sp->GetByteSize());
1611                 formatters::StringPrinter::ReadBufferAndDumpToStream<lldb_private::formatters::StringPrinter::StringElementType::ASCII>(options);
1612                 return !error.Fail();
1613             }
1614 
1615             if (custom_format == eFormatEnum)
1616                 return false;
1617 
1618             // this only works for arrays, because I have no way to know when
1619             // the pointed memory ends, and no special \0 end of data marker
1620             if (flags.Test(eTypeIsArray))
1621             {
1622                 if ((custom_format == eFormatBytes) ||
1623                     (custom_format == eFormatBytesWithASCII))
1624                 {
1625                     const size_t count = GetNumChildren();
1626 
1627                     s << '[';
1628                     for (size_t low = 0; low < count; low++)
1629                     {
1630 
1631                         if (low)
1632                             s << ',';
1633 
1634                         ValueObjectSP child = GetChildAtIndex(low,true);
1635                         if (!child.get())
1636                         {
1637                             s << "<invalid child>";
1638                             continue;
1639                         }
1640                         child->DumpPrintableRepresentation(s, ValueObject::eValueObjectRepresentationStyleValue, custom_format);
1641                     }
1642 
1643                     s << ']';
1644 
1645                     return true;
1646                 }
1647 
1648                 if ((custom_format == eFormatVectorOfChar) ||
1649                     (custom_format == eFormatVectorOfFloat32) ||
1650                     (custom_format == eFormatVectorOfFloat64) ||
1651                     (custom_format == eFormatVectorOfSInt16) ||
1652                     (custom_format == eFormatVectorOfSInt32) ||
1653                     (custom_format == eFormatVectorOfSInt64) ||
1654                     (custom_format == eFormatVectorOfSInt8) ||
1655                     (custom_format == eFormatVectorOfUInt128) ||
1656                     (custom_format == eFormatVectorOfUInt16) ||
1657                     (custom_format == eFormatVectorOfUInt32) ||
1658                     (custom_format == eFormatVectorOfUInt64) ||
1659                     (custom_format == eFormatVectorOfUInt8)) // arrays of bytes, bytes with ASCII or any vector format should be printed directly
1660                 {
1661                     const size_t count = GetNumChildren();
1662 
1663                     Format format = FormatManager::GetSingleItemFormat(custom_format);
1664 
1665                     s << '[';
1666                     for (size_t low = 0; low < count; low++)
1667                     {
1668 
1669                         if (low)
1670                             s << ',';
1671 
1672                         ValueObjectSP child = GetChildAtIndex(low,true);
1673                         if (!child.get())
1674                         {
1675                             s << "<invalid child>";
1676                             continue;
1677                         }
1678                         child->DumpPrintableRepresentation(s, ValueObject::eValueObjectRepresentationStyleValue, format);
1679                     }
1680 
1681                     s << ']';
1682 
1683                     return true;
1684                 }
1685             }
1686 
1687             if ((custom_format == eFormatBoolean) ||
1688                 (custom_format == eFormatBinary) ||
1689                 (custom_format == eFormatChar) ||
1690                 (custom_format == eFormatCharPrintable) ||
1691                 (custom_format == eFormatComplexFloat) ||
1692                 (custom_format == eFormatDecimal) ||
1693                 (custom_format == eFormatHex) ||
1694                 (custom_format == eFormatHexUppercase) ||
1695                 (custom_format == eFormatFloat) ||
1696                 (custom_format == eFormatOctal) ||
1697                 (custom_format == eFormatOSType) ||
1698                 (custom_format == eFormatUnicode16) ||
1699                 (custom_format == eFormatUnicode32) ||
1700                 (custom_format == eFormatUnsigned) ||
1701                 (custom_format == eFormatPointer) ||
1702                 (custom_format == eFormatComplexInteger) ||
1703                 (custom_format == eFormatComplex) ||
1704                 (custom_format == eFormatDefault)) // use the [] operator
1705                 return false;
1706         }
1707     }
1708 
1709     if (only_special)
1710         return false;
1711 
1712     bool var_success = false;
1713 
1714     {
1715         const char *cstr = NULL;
1716 
1717          // this is a local stream that we are using to ensure that the data pointed to by cstr survives
1718         // long enough for us to copy it to its destination - it is necessary to have this temporary storage
1719         // area for cases where our desired output is not backed by some other longer-term storage
1720         StreamString strm;
1721 
1722         if (custom_format != eFormatInvalid)
1723             SetFormat(custom_format);
1724 
1725         switch(val_obj_display)
1726         {
1727             case eValueObjectRepresentationStyleValue:
1728                 cstr = GetValueAsCString();
1729                 break;
1730 
1731             case eValueObjectRepresentationStyleSummary:
1732                 cstr = GetSummaryAsCString();
1733                 break;
1734 
1735             case eValueObjectRepresentationStyleLanguageSpecific:
1736                 cstr = GetObjectDescription();
1737                 break;
1738 
1739             case eValueObjectRepresentationStyleLocation:
1740                 cstr = GetLocationAsCString();
1741                 break;
1742 
1743             case eValueObjectRepresentationStyleChildrenCount:
1744                 strm.Printf("%" PRIu64 "", (uint64_t)GetNumChildren());
1745                 cstr = strm.GetString().c_str();
1746                 break;
1747 
1748             case eValueObjectRepresentationStyleType:
1749                 cstr = GetTypeName().AsCString();
1750                 break;
1751 
1752             case eValueObjectRepresentationStyleName:
1753                 cstr = GetName().AsCString();
1754                 break;
1755 
1756             case eValueObjectRepresentationStyleExpressionPath:
1757                 GetExpressionPath(strm, false);
1758                 cstr = strm.GetString().c_str();
1759                 break;
1760         }
1761 
1762         if (!cstr)
1763         {
1764             if (val_obj_display == eValueObjectRepresentationStyleValue)
1765                 cstr = GetSummaryAsCString();
1766             else if (val_obj_display == eValueObjectRepresentationStyleSummary)
1767             {
1768                 if (!CanProvideValue())
1769                 {
1770                     strm.Printf("%s @ %s", GetTypeName().AsCString(), GetLocationAsCString());
1771                     cstr = strm.GetString().c_str();
1772                 }
1773                 else
1774                     cstr = GetValueAsCString();
1775             }
1776         }
1777 
1778         if (cstr)
1779             s.PutCString(cstr);
1780         else
1781         {
1782             if (m_error.Fail())
1783             {
1784                 if (do_dump_error)
1785                     s.Printf("<%s>", m_error.AsCString());
1786                 else
1787                     return false;
1788             }
1789             else if (val_obj_display == eValueObjectRepresentationStyleSummary)
1790                 s.PutCString("<no summary available>");
1791             else if (val_obj_display == eValueObjectRepresentationStyleValue)
1792                 s.PutCString("<no value available>");
1793             else if (val_obj_display == eValueObjectRepresentationStyleLanguageSpecific)
1794                 s.PutCString("<not a valid Objective-C object>"); // edit this if we have other runtimes that support a description
1795             else
1796                 s.PutCString("<no printable representation>");
1797         }
1798 
1799         // we should only return false here if we could not do *anything*
1800         // even if we have an error message as output, that's a success
1801         // from our callers' perspective, so return true
1802         var_success = true;
1803 
1804         if (custom_format != eFormatInvalid)
1805             SetFormat(eFormatDefault);
1806     }
1807 
1808     return var_success;
1809 }
1810 
1811 addr_t
1812 ValueObject::GetAddressOf (bool scalar_is_load_address, AddressType *address_type)
1813 {
1814     if (!UpdateValueIfNeeded(false))
1815         return LLDB_INVALID_ADDRESS;
1816 
1817     switch (m_value.GetValueType())
1818     {
1819     case Value::eValueTypeScalar:
1820     case Value::eValueTypeVector:
1821         if (scalar_is_load_address)
1822         {
1823             if(address_type)
1824                 *address_type = eAddressTypeLoad;
1825             return m_value.GetScalar().ULongLong(LLDB_INVALID_ADDRESS);
1826         }
1827         break;
1828 
1829     case Value::eValueTypeLoadAddress:
1830     case Value::eValueTypeFileAddress:
1831         {
1832             if(address_type)
1833                 *address_type = m_value.GetValueAddressType ();
1834             return m_value.GetScalar().ULongLong(LLDB_INVALID_ADDRESS);
1835         }
1836         break;
1837     case Value::eValueTypeHostAddress:
1838         {
1839             if(address_type)
1840                 *address_type = m_value.GetValueAddressType ();
1841             return LLDB_INVALID_ADDRESS;
1842         }
1843         break;
1844     }
1845     if (address_type)
1846         *address_type = eAddressTypeInvalid;
1847     return LLDB_INVALID_ADDRESS;
1848 }
1849 
1850 addr_t
1851 ValueObject::GetPointerValue (AddressType *address_type)
1852 {
1853     addr_t address = LLDB_INVALID_ADDRESS;
1854     if(address_type)
1855         *address_type = eAddressTypeInvalid;
1856 
1857     if (!UpdateValueIfNeeded(false))
1858         return address;
1859 
1860     switch (m_value.GetValueType())
1861     {
1862     case Value::eValueTypeScalar:
1863     case Value::eValueTypeVector:
1864         address = m_value.GetScalar().ULongLong(LLDB_INVALID_ADDRESS);
1865         break;
1866 
1867     case Value::eValueTypeHostAddress:
1868     case Value::eValueTypeLoadAddress:
1869     case Value::eValueTypeFileAddress:
1870         {
1871             lldb::offset_t data_offset = 0;
1872             address = m_data.GetPointer(&data_offset);
1873         }
1874         break;
1875     }
1876 
1877     if (address_type)
1878         *address_type = GetAddressTypeOfChildren();
1879 
1880     return address;
1881 }
1882 
1883 bool
1884 ValueObject::SetValueFromCString (const char *value_str, Error& error)
1885 {
1886     error.Clear();
1887     // Make sure our value is up to date first so that our location and location
1888     // type is valid.
1889     if (!UpdateValueIfNeeded(false))
1890     {
1891         error.SetErrorString("unable to read value");
1892         return false;
1893     }
1894 
1895     uint64_t count = 0;
1896     const Encoding encoding = GetCompilerType().GetEncoding (count);
1897 
1898     const size_t byte_size = GetByteSize();
1899 
1900     Value::ValueType value_type = m_value.GetValueType();
1901 
1902     if (value_type == Value::eValueTypeScalar)
1903     {
1904         // If the value is already a scalar, then let the scalar change itself:
1905         m_value.GetScalar().SetValueFromCString (value_str, encoding, byte_size);
1906     }
1907     else if (byte_size <= 16)
1908     {
1909         // If the value fits in a scalar, then make a new scalar and again let the
1910         // scalar code do the conversion, then figure out where to put the new value.
1911         Scalar new_scalar;
1912         error = new_scalar.SetValueFromCString (value_str, encoding, byte_size);
1913         if (error.Success())
1914         {
1915             switch (value_type)
1916             {
1917             case Value::eValueTypeLoadAddress:
1918                 {
1919                     // If it is a load address, then the scalar value is the storage location
1920                     // of the data, and we have to shove this value down to that load location.
1921                     ExecutionContext exe_ctx (GetExecutionContextRef());
1922                     Process *process = exe_ctx.GetProcessPtr();
1923                     if (process)
1924                     {
1925                         addr_t target_addr = m_value.GetScalar().ULongLong(LLDB_INVALID_ADDRESS);
1926                         size_t bytes_written = process->WriteScalarToMemory (target_addr,
1927                                                                              new_scalar,
1928                                                                              byte_size,
1929                                                                              error);
1930                         if (!error.Success())
1931                             return false;
1932                         if (bytes_written != byte_size)
1933                         {
1934                             error.SetErrorString("unable to write value to memory");
1935                             return false;
1936                         }
1937                     }
1938                 }
1939                 break;
1940             case Value::eValueTypeHostAddress:
1941                 {
1942                     // If it is a host address, then we stuff the scalar as a DataBuffer into the Value's data.
1943                     DataExtractor new_data;
1944                     new_data.SetByteOrder (m_data.GetByteOrder());
1945 
1946                     DataBufferSP buffer_sp (new DataBufferHeap(byte_size, 0));
1947                     m_data.SetData(buffer_sp, 0);
1948                     bool success = new_scalar.GetData(new_data);
1949                     if (success)
1950                     {
1951                         new_data.CopyByteOrderedData (0,
1952                                                       byte_size,
1953                                                       const_cast<uint8_t *>(m_data.GetDataStart()),
1954                                                       byte_size,
1955                                                       m_data.GetByteOrder());
1956                     }
1957                     m_value.GetScalar() = (uintptr_t)m_data.GetDataStart();
1958 
1959                 }
1960                 break;
1961             case Value::eValueTypeFileAddress:
1962             case Value::eValueTypeScalar:
1963             case Value::eValueTypeVector:
1964                 break;
1965             }
1966         }
1967         else
1968         {
1969             return false;
1970         }
1971     }
1972     else
1973     {
1974         // We don't support setting things bigger than a scalar at present.
1975         error.SetErrorString("unable to write aggregate data type");
1976         return false;
1977     }
1978 
1979     // If we have reached this point, then we have successfully changed the value.
1980     SetNeedsUpdate();
1981     return true;
1982 }
1983 
1984 bool
1985 ValueObject::GetDeclaration (Declaration &decl)
1986 {
1987     decl.Clear();
1988     return false;
1989 }
1990 
1991 ConstString
1992 ValueObject::GetTypeName()
1993 {
1994     return GetCompilerType().GetConstTypeName();
1995 }
1996 
1997 ConstString
1998 ValueObject::GetDisplayTypeName()
1999 {
2000     return GetTypeName();
2001 }
2002 
2003 ConstString
2004 ValueObject::GetQualifiedTypeName()
2005 {
2006     return GetCompilerType().GetConstQualifiedTypeName();
2007 }
2008 
2009 
2010 LanguageType
2011 ValueObject::GetObjectRuntimeLanguage ()
2012 {
2013     return GetCompilerType().GetMinimumLanguage ();
2014 }
2015 
2016 void
2017 ValueObject::AddSyntheticChild (const ConstString &key, ValueObject *valobj)
2018 {
2019     m_synthetic_children[key] = valobj;
2020 }
2021 
2022 ValueObjectSP
2023 ValueObject::GetSyntheticChild (const ConstString &key) const
2024 {
2025     ValueObjectSP synthetic_child_sp;
2026     std::map<ConstString, ValueObject *>::const_iterator pos = m_synthetic_children.find (key);
2027     if (pos != m_synthetic_children.end())
2028         synthetic_child_sp = pos->second->GetSP();
2029     return synthetic_child_sp;
2030 }
2031 
2032 uint32_t
2033 ValueObject::GetTypeInfo (CompilerType *pointee_or_element_compiler_type)
2034 {
2035     return GetCompilerType().GetTypeInfo (pointee_or_element_compiler_type);
2036 }
2037 
2038 bool
2039 ValueObject::IsPointerType ()
2040 {
2041     return GetCompilerType().IsPointerType();
2042 }
2043 
2044 bool
2045 ValueObject::IsArrayType ()
2046 {
2047     return GetCompilerType().IsArrayType (NULL, NULL, NULL);
2048 }
2049 
2050 bool
2051 ValueObject::IsScalarType ()
2052 {
2053     return GetCompilerType().IsScalarType ();
2054 }
2055 
2056 bool
2057 ValueObject::IsIntegerType (bool &is_signed)
2058 {
2059     return GetCompilerType().IsIntegerType (is_signed);
2060 }
2061 
2062 bool
2063 ValueObject::IsPointerOrReferenceType ()
2064 {
2065     return GetCompilerType().IsPointerOrReferenceType ();
2066 }
2067 
2068 bool
2069 ValueObject::IsPossibleDynamicType ()
2070 {
2071     ExecutionContext exe_ctx (GetExecutionContextRef());
2072     Process *process = exe_ctx.GetProcessPtr();
2073     if (process)
2074         return process->IsPossibleDynamicValue(*this);
2075     else
2076         return GetCompilerType().IsPossibleDynamicType (NULL, true, true);
2077 }
2078 
2079 bool
2080 ValueObject::IsRuntimeSupportValue ()
2081 {
2082     Process *process(GetProcessSP().get());
2083     if (process)
2084     {
2085         LanguageRuntime *runtime = process->GetLanguageRuntime(GetObjectRuntimeLanguage());
2086         if (!runtime)
2087             runtime = process->GetObjCLanguageRuntime();
2088         if (runtime)
2089             return runtime->IsRuntimeSupportValue(*this);
2090     }
2091     return false;
2092 }
2093 
2094 bool
2095 ValueObject::IsObjCNil ()
2096 {
2097     const uint32_t mask = eTypeIsObjC | eTypeIsPointer;
2098     bool isObjCpointer = (((GetCompilerType().GetTypeInfo(NULL)) & mask) == mask);
2099     if (!isObjCpointer)
2100         return false;
2101     bool canReadValue = true;
2102     bool isZero = GetValueAsUnsigned(0,&canReadValue) == 0;
2103     return canReadValue && isZero;
2104 }
2105 
2106 // This allows you to create an array member using and index
2107 // that doesn't not fall in the normal bounds of the array.
2108 // Many times structure can be defined as:
2109 // struct Collection
2110 // {
2111 //     uint32_t item_count;
2112 //     Item item_array[0];
2113 // };
2114 // The size of the "item_array" is 1, but many times in practice
2115 // there are more items in "item_array".
2116 
2117 ValueObjectSP
2118 ValueObject::GetSyntheticArrayMember (size_t index, bool can_create)
2119 {
2120     ValueObjectSP synthetic_child_sp;
2121     if (IsPointerType () || IsArrayType())
2122     {
2123         char index_str[64];
2124         snprintf(index_str, sizeof(index_str), "[%" PRIu64 "]", (uint64_t)index);
2125         ConstString index_const_str(index_str);
2126         // Check if we have already created a synthetic array member in this
2127         // valid object. If we have we will re-use it.
2128         synthetic_child_sp = GetSyntheticChild (index_const_str);
2129         if (!synthetic_child_sp)
2130         {
2131             ValueObject *synthetic_child;
2132             // We haven't made a synthetic array member for INDEX yet, so
2133             // lets make one and cache it for any future reference.
2134             synthetic_child = CreateChildAtIndex(0, true, index);
2135 
2136             // Cache the value if we got one back...
2137             if (synthetic_child)
2138             {
2139                 AddSyntheticChild(index_const_str, synthetic_child);
2140                 synthetic_child_sp = synthetic_child->GetSP();
2141                 synthetic_child_sp->SetName(ConstString(index_str));
2142                 synthetic_child_sp->m_is_array_item_for_pointer = true;
2143             }
2144         }
2145     }
2146     return synthetic_child_sp;
2147 }
2148 
2149 ValueObjectSP
2150 ValueObject::GetSyntheticBitFieldChild (uint32_t from, uint32_t to, bool can_create)
2151 {
2152     ValueObjectSP synthetic_child_sp;
2153     if (IsScalarType ())
2154     {
2155         char index_str[64];
2156         snprintf(index_str, sizeof(index_str), "[%i-%i]", from, to);
2157         ConstString index_const_str(index_str);
2158         // Check if we have already created a synthetic array member in this
2159         // valid object. If we have we will re-use it.
2160         synthetic_child_sp = GetSyntheticChild (index_const_str);
2161         if (!synthetic_child_sp)
2162         {
2163             // We haven't made a synthetic array member for INDEX yet, so
2164             // lets make one and cache it for any future reference.
2165             ValueObjectChild *synthetic_child = new ValueObjectChild (*this,
2166                                                                       GetCompilerType(),
2167                                                                       index_const_str,
2168                                                                       GetByteSize(),
2169                                                                       0,
2170                                                                       to-from+1,
2171                                                                       from,
2172                                                                       false,
2173                                                                       false,
2174                                                                       eAddressTypeInvalid);
2175 
2176             // Cache the value if we got one back...
2177             if (synthetic_child)
2178             {
2179                 AddSyntheticChild(index_const_str, synthetic_child);
2180                 synthetic_child_sp = synthetic_child->GetSP();
2181                 synthetic_child_sp->SetName(ConstString(index_str));
2182                 synthetic_child_sp->m_is_bitfield_for_scalar = true;
2183             }
2184         }
2185     }
2186     return synthetic_child_sp;
2187 }
2188 
2189 ValueObjectSP
2190 ValueObject::GetSyntheticChildAtOffset(uint32_t offset, const CompilerType& type, bool can_create)
2191 {
2192 
2193     ValueObjectSP synthetic_child_sp;
2194 
2195     char name_str[64];
2196     snprintf(name_str, sizeof(name_str), "@%i", offset);
2197     ConstString name_const_str(name_str);
2198 
2199     // Check if we have already created a synthetic array member in this
2200     // valid object. If we have we will re-use it.
2201     synthetic_child_sp = GetSyntheticChild (name_const_str);
2202 
2203     if (synthetic_child_sp.get())
2204         return synthetic_child_sp;
2205 
2206     if (!can_create)
2207         return ValueObjectSP();
2208 
2209     ExecutionContext exe_ctx (GetExecutionContextRef());
2210 
2211     ValueObjectChild *synthetic_child = new ValueObjectChild(*this,
2212                                                              type,
2213                                                              name_const_str,
2214                                                              type.GetByteSize(exe_ctx.GetBestExecutionContextScope()),
2215                                                              offset,
2216                                                              0,
2217                                                              0,
2218                                                              false,
2219                                                              false,
2220                                                              eAddressTypeInvalid);
2221     if (synthetic_child)
2222     {
2223         AddSyntheticChild(name_const_str, synthetic_child);
2224         synthetic_child_sp = synthetic_child->GetSP();
2225         synthetic_child_sp->SetName(name_const_str);
2226         synthetic_child_sp->m_is_child_at_offset = true;
2227     }
2228     return synthetic_child_sp;
2229 }
2230 
2231 ValueObjectSP
2232 ValueObject::GetSyntheticBase (uint32_t offset, const CompilerType& type, bool can_create)
2233 {
2234     ValueObjectSP synthetic_child_sp;
2235 
2236     char name_str[64];
2237     snprintf(name_str, sizeof(name_str), "%s", type.GetTypeName().AsCString("<unknown>"));
2238     ConstString name_const_str(name_str);
2239 
2240     // Check if we have already created a synthetic array member in this
2241     // valid object. If we have we will re-use it.
2242     synthetic_child_sp = GetSyntheticChild (name_const_str);
2243 
2244     if (synthetic_child_sp.get())
2245         return synthetic_child_sp;
2246 
2247     if (!can_create)
2248         return ValueObjectSP();
2249 
2250     const bool is_base_class = true;
2251 
2252     ExecutionContext exe_ctx (GetExecutionContextRef());
2253 
2254     ValueObjectChild *synthetic_child = new ValueObjectChild(*this,
2255                                                              type,
2256                                                              name_const_str,
2257                                                              type.GetByteSize(exe_ctx.GetBestExecutionContextScope()),
2258                                                              offset,
2259                                                              0,
2260                                                              0,
2261                                                              is_base_class,
2262                                                              false,
2263                                                              eAddressTypeInvalid);
2264     if (synthetic_child)
2265     {
2266         AddSyntheticChild(name_const_str, synthetic_child);
2267         synthetic_child_sp = synthetic_child->GetSP();
2268         synthetic_child_sp->SetName(name_const_str);
2269     }
2270     return synthetic_child_sp;
2271 }
2272 
2273 
2274 // your expression path needs to have a leading . or ->
2275 // (unless it somehow "looks like" an array, in which case it has
2276 // a leading [ symbol). while the [ is meaningful and should be shown
2277 // to the user, . and -> are just parser design, but by no means
2278 // added information for the user.. strip them off
2279 static const char*
2280 SkipLeadingExpressionPathSeparators(const char* expression)
2281 {
2282     if (!expression || !expression[0])
2283         return expression;
2284     if (expression[0] == '.')
2285         return expression+1;
2286     if (expression[0] == '-' && expression[1] == '>')
2287         return expression+2;
2288     return expression;
2289 }
2290 
2291 ValueObjectSP
2292 ValueObject::GetSyntheticExpressionPathChild(const char* expression, bool can_create)
2293 {
2294     ValueObjectSP synthetic_child_sp;
2295     ConstString name_const_string(expression);
2296     // Check if we have already created a synthetic array member in this
2297     // valid object. If we have we will re-use it.
2298     synthetic_child_sp = GetSyntheticChild (name_const_string);
2299     if (!synthetic_child_sp)
2300     {
2301         // We haven't made a synthetic array member for expression yet, so
2302         // lets make one and cache it for any future reference.
2303         synthetic_child_sp = GetValueForExpressionPath(expression,
2304                                                        NULL, NULL, NULL,
2305                                                        GetValueForExpressionPathOptions().SetSyntheticChildrenTraversal(GetValueForExpressionPathOptions::SyntheticChildrenTraversal::None));
2306 
2307         // Cache the value if we got one back...
2308         if (synthetic_child_sp.get())
2309         {
2310             // FIXME: this causes a "real" child to end up with its name changed to the contents of expression
2311             AddSyntheticChild(name_const_string, synthetic_child_sp.get());
2312             synthetic_child_sp->SetName(ConstString(SkipLeadingExpressionPathSeparators(expression)));
2313         }
2314     }
2315     return synthetic_child_sp;
2316 }
2317 
2318 void
2319 ValueObject::CalculateSyntheticValue (bool use_synthetic)
2320 {
2321     if (use_synthetic == false)
2322         return;
2323 
2324     TargetSP target_sp(GetTargetSP());
2325     if (target_sp && target_sp->GetEnableSyntheticValue() == false)
2326     {
2327         m_synthetic_value = NULL;
2328         return;
2329     }
2330 
2331     lldb::SyntheticChildrenSP current_synth_sp(m_synthetic_children_sp);
2332 
2333     if (!UpdateFormatsIfNeeded() && m_synthetic_value)
2334         return;
2335 
2336     if (m_synthetic_children_sp.get() == NULL)
2337         return;
2338 
2339     if (current_synth_sp == m_synthetic_children_sp && m_synthetic_value)
2340         return;
2341 
2342     m_synthetic_value = new ValueObjectSynthetic(*this, m_synthetic_children_sp);
2343 }
2344 
2345 void
2346 ValueObject::CalculateDynamicValue (DynamicValueType use_dynamic)
2347 {
2348     if (use_dynamic == eNoDynamicValues)
2349         return;
2350 
2351     if (!m_dynamic_value && !IsDynamic())
2352     {
2353         ExecutionContext exe_ctx (GetExecutionContextRef());
2354         Process *process = exe_ctx.GetProcessPtr();
2355         if (process && process->IsPossibleDynamicValue(*this))
2356         {
2357             ClearDynamicTypeInformation ();
2358             m_dynamic_value = new ValueObjectDynamicValue (*this, use_dynamic);
2359         }
2360     }
2361 }
2362 
2363 ValueObjectSP
2364 ValueObject::GetDynamicValue (DynamicValueType use_dynamic)
2365 {
2366     if (use_dynamic == eNoDynamicValues)
2367         return ValueObjectSP();
2368 
2369     if (!IsDynamic() && m_dynamic_value == NULL)
2370     {
2371         CalculateDynamicValue(use_dynamic);
2372     }
2373     if (m_dynamic_value)
2374         return m_dynamic_value->GetSP();
2375     else
2376         return ValueObjectSP();
2377 }
2378 
2379 ValueObjectSP
2380 ValueObject::GetStaticValue()
2381 {
2382     return GetSP();
2383 }
2384 
2385 lldb::ValueObjectSP
2386 ValueObject::GetNonSyntheticValue ()
2387 {
2388     return GetSP();
2389 }
2390 
2391 ValueObjectSP
2392 ValueObject::GetSyntheticValue (bool use_synthetic)
2393 {
2394     if (use_synthetic == false)
2395         return ValueObjectSP();
2396 
2397     CalculateSyntheticValue(use_synthetic);
2398 
2399     if (m_synthetic_value)
2400         return m_synthetic_value->GetSP();
2401     else
2402         return ValueObjectSP();
2403 }
2404 
2405 bool
2406 ValueObject::HasSyntheticValue()
2407 {
2408     UpdateFormatsIfNeeded();
2409 
2410     if (m_synthetic_children_sp.get() == NULL)
2411         return false;
2412 
2413     CalculateSyntheticValue(true);
2414 
2415     if (m_synthetic_value)
2416         return true;
2417     else
2418         return false;
2419 }
2420 
2421 bool
2422 ValueObject::GetBaseClassPath (Stream &s)
2423 {
2424     if (IsBaseClass())
2425     {
2426         bool parent_had_base_class = GetParent() && GetParent()->GetBaseClassPath (s);
2427         CompilerType compiler_type = GetCompilerType();
2428         std::string cxx_class_name;
2429         bool this_had_base_class = ClangASTContext::GetCXXClassName (compiler_type, cxx_class_name);
2430         if (this_had_base_class)
2431         {
2432             if (parent_had_base_class)
2433                 s.PutCString("::");
2434             s.PutCString(cxx_class_name.c_str());
2435         }
2436         return parent_had_base_class || this_had_base_class;
2437     }
2438     return false;
2439 }
2440 
2441 
2442 ValueObject *
2443 ValueObject::GetNonBaseClassParent()
2444 {
2445     if (GetParent())
2446     {
2447         if (GetParent()->IsBaseClass())
2448             return GetParent()->GetNonBaseClassParent();
2449         else
2450             return GetParent();
2451     }
2452     return NULL;
2453 }
2454 
2455 
2456 bool
2457 ValueObject::IsBaseClass (uint32_t& depth)
2458 {
2459     if (!IsBaseClass())
2460     {
2461         depth = 0;
2462         return false;
2463     }
2464     if (GetParent())
2465     {
2466         GetParent()->IsBaseClass(depth);
2467         depth = depth + 1;
2468         return true;
2469     }
2470     // TODO: a base of no parent? weird..
2471     depth = 1;
2472     return true;
2473 }
2474 
2475 void
2476 ValueObject::GetExpressionPath (Stream &s, bool qualify_cxx_base_classes, GetExpressionPathFormat epformat)
2477 {
2478     // synthetic children do not actually "exist" as part of the hierarchy, and sometimes they are consed up in ways
2479     // that don't make sense from an underlying language/API standpoint. So, use a special code path here to return
2480     // something that can hopefully be used in expression
2481     if (m_is_synthetic_children_generated)
2482     {
2483         UpdateValueIfNeeded();
2484 
2485         if (m_value.GetValueType() == Value::eValueTypeLoadAddress)
2486         {
2487             if (IsPointerOrReferenceType())
2488             {
2489                 s.Printf("((%s)0x%" PRIx64 ")",
2490                          GetTypeName().AsCString("void"),
2491                          GetValueAsUnsigned(0));
2492                 return;
2493             }
2494             else
2495             {
2496                 uint64_t load_addr = m_value.GetScalar().ULongLong(LLDB_INVALID_ADDRESS);
2497                 if (load_addr != LLDB_INVALID_ADDRESS)
2498                 {
2499                     s.Printf("(*( (%s *)0x%" PRIx64 "))",
2500                              GetTypeName().AsCString("void"),
2501                              load_addr);
2502                     return;
2503                 }
2504             }
2505         }
2506 
2507         if (CanProvideValue())
2508         {
2509             s.Printf("((%s)%s)",
2510                      GetTypeName().AsCString("void"),
2511                      GetValueAsCString());
2512             return;
2513         }
2514 
2515         return;
2516     }
2517 
2518     const bool is_deref_of_parent = IsDereferenceOfParent ();
2519 
2520     if (is_deref_of_parent && epformat == eGetExpressionPathFormatDereferencePointers)
2521     {
2522         // this is the original format of GetExpressionPath() producing code like *(a_ptr).memberName, which is entirely
2523         // fine, until you put this into StackFrame::GetValueForVariableExpressionPath() which prefers to see a_ptr->memberName.
2524         // the eHonorPointers mode is meant to produce strings in this latter format
2525         s.PutCString("*(");
2526     }
2527 
2528     ValueObject* parent = GetParent();
2529 
2530     if (parent)
2531         parent->GetExpressionPath (s, qualify_cxx_base_classes, epformat);
2532 
2533     // if we are a deref_of_parent just because we are synthetic array
2534     // members made up to allow ptr[%d] syntax to work in variable
2535     // printing, then add our name ([%d]) to the expression path
2536     if (m_is_array_item_for_pointer && epformat == eGetExpressionPathFormatHonorPointers)
2537         s.PutCString(m_name.AsCString());
2538 
2539     if (!IsBaseClass())
2540     {
2541         if (!is_deref_of_parent)
2542         {
2543             ValueObject *non_base_class_parent = GetNonBaseClassParent();
2544             if (non_base_class_parent)
2545             {
2546                 CompilerType non_base_class_parent_compiler_type = non_base_class_parent->GetCompilerType();
2547                 if (non_base_class_parent_compiler_type)
2548                 {
2549                     if (parent && parent->IsDereferenceOfParent() && epformat == eGetExpressionPathFormatHonorPointers)
2550                     {
2551                         s.PutCString("->");
2552                     }
2553                     else
2554                     {
2555                         const uint32_t non_base_class_parent_type_info = non_base_class_parent_compiler_type.GetTypeInfo();
2556 
2557                         if (non_base_class_parent_type_info & eTypeIsPointer)
2558                         {
2559                             s.PutCString("->");
2560                         }
2561                         else if ((non_base_class_parent_type_info & eTypeHasChildren) &&
2562                                  !(non_base_class_parent_type_info & eTypeIsArray))
2563                         {
2564                             s.PutChar('.');
2565                         }
2566                     }
2567                 }
2568             }
2569 
2570             const char *name = GetName().GetCString();
2571             if (name)
2572             {
2573                 if (qualify_cxx_base_classes)
2574                 {
2575                     if (GetBaseClassPath (s))
2576                         s.PutCString("::");
2577                 }
2578                 s.PutCString(name);
2579             }
2580         }
2581     }
2582 
2583     if (is_deref_of_parent && epformat == eGetExpressionPathFormatDereferencePointers)
2584     {
2585         s.PutChar(')');
2586     }
2587 }
2588 
2589 ValueObjectSP
2590 ValueObject::GetValueForExpressionPath(const char* expression,
2591                                        const char** first_unparsed,
2592                                        ExpressionPathScanEndReason* reason_to_stop,
2593                                        ExpressionPathEndResultType* final_value_type,
2594                                        const GetValueForExpressionPathOptions& options,
2595                                        ExpressionPathAftermath* final_task_on_target)
2596 {
2597 
2598     const char* dummy_first_unparsed;
2599     ExpressionPathScanEndReason dummy_reason_to_stop = ValueObject::eExpressionPathScanEndReasonUnknown;
2600     ExpressionPathEndResultType dummy_final_value_type = ValueObject::eExpressionPathEndResultTypeInvalid;
2601     ExpressionPathAftermath dummy_final_task_on_target = ValueObject::eExpressionPathAftermathNothing;
2602 
2603     ValueObjectSP ret_val = GetValueForExpressionPath_Impl(expression,
2604                                                            first_unparsed ? first_unparsed : &dummy_first_unparsed,
2605                                                            reason_to_stop ? reason_to_stop : &dummy_reason_to_stop,
2606                                                            final_value_type ? final_value_type : &dummy_final_value_type,
2607                                                            options,
2608                                                            final_task_on_target ? final_task_on_target : &dummy_final_task_on_target);
2609 
2610     if (!final_task_on_target || *final_task_on_target == ValueObject::eExpressionPathAftermathNothing)
2611         return ret_val;
2612 
2613     if (ret_val.get() && ((final_value_type ? *final_value_type : dummy_final_value_type) == eExpressionPathEndResultTypePlain)) // I can only deref and takeaddress of plain objects
2614     {
2615         if ( (final_task_on_target ? *final_task_on_target : dummy_final_task_on_target) == ValueObject::eExpressionPathAftermathDereference)
2616         {
2617             Error error;
2618             ValueObjectSP final_value = ret_val->Dereference(error);
2619             if (error.Fail() || !final_value.get())
2620             {
2621                 if (reason_to_stop)
2622                     *reason_to_stop = ValueObject::eExpressionPathScanEndReasonDereferencingFailed;
2623                 if (final_value_type)
2624                     *final_value_type = ValueObject::eExpressionPathEndResultTypeInvalid;
2625                 return ValueObjectSP();
2626             }
2627             else
2628             {
2629                 if (final_task_on_target)
2630                     *final_task_on_target = ValueObject::eExpressionPathAftermathNothing;
2631                 return final_value;
2632             }
2633         }
2634         if (*final_task_on_target == ValueObject::eExpressionPathAftermathTakeAddress)
2635         {
2636             Error error;
2637             ValueObjectSP final_value = ret_val->AddressOf(error);
2638             if (error.Fail() || !final_value.get())
2639             {
2640                 if (reason_to_stop)
2641                     *reason_to_stop = ValueObject::eExpressionPathScanEndReasonTakingAddressFailed;
2642                 if (final_value_type)
2643                     *final_value_type = ValueObject::eExpressionPathEndResultTypeInvalid;
2644                 return ValueObjectSP();
2645             }
2646             else
2647             {
2648                 if (final_task_on_target)
2649                     *final_task_on_target = ValueObject::eExpressionPathAftermathNothing;
2650                 return final_value;
2651             }
2652         }
2653     }
2654     return ret_val; // final_task_on_target will still have its original value, so you know I did not do it
2655 }
2656 
2657 int
2658 ValueObject::GetValuesForExpressionPath(const char* expression,
2659                                         ValueObjectListSP& list,
2660                                         const char** first_unparsed,
2661                                         ExpressionPathScanEndReason* reason_to_stop,
2662                                         ExpressionPathEndResultType* final_value_type,
2663                                         const GetValueForExpressionPathOptions& options,
2664                                         ExpressionPathAftermath* final_task_on_target)
2665 {
2666     const char* dummy_first_unparsed;
2667     ExpressionPathScanEndReason dummy_reason_to_stop;
2668     ExpressionPathEndResultType dummy_final_value_type;
2669     ExpressionPathAftermath dummy_final_task_on_target = ValueObject::eExpressionPathAftermathNothing;
2670 
2671     ValueObjectSP ret_val = GetValueForExpressionPath_Impl(expression,
2672                                                            first_unparsed ? first_unparsed : &dummy_first_unparsed,
2673                                                            reason_to_stop ? reason_to_stop : &dummy_reason_to_stop,
2674                                                            final_value_type ? final_value_type : &dummy_final_value_type,
2675                                                            options,
2676                                                            final_task_on_target ? final_task_on_target : &dummy_final_task_on_target);
2677 
2678     if (!ret_val.get()) // if there are errors, I add nothing to the list
2679         return 0;
2680 
2681     if ( (reason_to_stop ? *reason_to_stop : dummy_reason_to_stop) != eExpressionPathScanEndReasonArrayRangeOperatorMet)
2682     {
2683         // I need not expand a range, just post-process the final value and return
2684         if (!final_task_on_target || *final_task_on_target == ValueObject::eExpressionPathAftermathNothing)
2685         {
2686             list->Append(ret_val);
2687             return 1;
2688         }
2689         if (ret_val.get() && (final_value_type ? *final_value_type : dummy_final_value_type) == eExpressionPathEndResultTypePlain) // I can only deref and takeaddress of plain objects
2690         {
2691             if (*final_task_on_target == ValueObject::eExpressionPathAftermathDereference)
2692             {
2693                 Error error;
2694                 ValueObjectSP final_value = ret_val->Dereference(error);
2695                 if (error.Fail() || !final_value.get())
2696                 {
2697                     if (reason_to_stop)
2698                         *reason_to_stop = ValueObject::eExpressionPathScanEndReasonDereferencingFailed;
2699                     if (final_value_type)
2700                         *final_value_type = ValueObject::eExpressionPathEndResultTypeInvalid;
2701                     return 0;
2702                 }
2703                 else
2704                 {
2705                     *final_task_on_target = ValueObject::eExpressionPathAftermathNothing;
2706                     list->Append(final_value);
2707                     return 1;
2708                 }
2709             }
2710             if (*final_task_on_target == ValueObject::eExpressionPathAftermathTakeAddress)
2711             {
2712                 Error error;
2713                 ValueObjectSP final_value = ret_val->AddressOf(error);
2714                 if (error.Fail() || !final_value.get())
2715                 {
2716                     if (reason_to_stop)
2717                         *reason_to_stop = ValueObject::eExpressionPathScanEndReasonTakingAddressFailed;
2718                     if (final_value_type)
2719                         *final_value_type = ValueObject::eExpressionPathEndResultTypeInvalid;
2720                     return 0;
2721                 }
2722                 else
2723                 {
2724                     *final_task_on_target = ValueObject::eExpressionPathAftermathNothing;
2725                     list->Append(final_value);
2726                     return 1;
2727                 }
2728             }
2729         }
2730     }
2731     else
2732     {
2733         return ExpandArraySliceExpression(first_unparsed ? *first_unparsed : dummy_first_unparsed,
2734                                           first_unparsed ? first_unparsed : &dummy_first_unparsed,
2735                                           ret_val,
2736                                           list,
2737                                           reason_to_stop ? reason_to_stop : &dummy_reason_to_stop,
2738                                           final_value_type ? final_value_type : &dummy_final_value_type,
2739                                           options,
2740                                           final_task_on_target ? final_task_on_target : &dummy_final_task_on_target);
2741     }
2742     // in any non-covered case, just do the obviously right thing
2743     list->Append(ret_val);
2744     return 1;
2745 }
2746 
2747 ValueObjectSP
2748 ValueObject::GetValueForExpressionPath_Impl(const char* expression_cstr,
2749                                             const char** first_unparsed,
2750                                             ExpressionPathScanEndReason* reason_to_stop,
2751                                             ExpressionPathEndResultType* final_result,
2752                                             const GetValueForExpressionPathOptions& options,
2753                                             ExpressionPathAftermath* what_next)
2754 {
2755     ValueObjectSP root = GetSP();
2756 
2757     if (!root.get())
2758         return ValueObjectSP();
2759 
2760     *first_unparsed = expression_cstr;
2761 
2762     while (true)
2763     {
2764 
2765         const char* expression_cstr = *first_unparsed; // hide the top level expression_cstr
2766 
2767         CompilerType root_compiler_type = root->GetCompilerType();
2768         CompilerType pointee_compiler_type;
2769         Flags pointee_compiler_type_info;
2770 
2771         Flags root_compiler_type_info(root_compiler_type.GetTypeInfo(&pointee_compiler_type));
2772         if (pointee_compiler_type)
2773             pointee_compiler_type_info.Reset(pointee_compiler_type.GetTypeInfo());
2774 
2775         if (!expression_cstr || *expression_cstr == '\0')
2776         {
2777             *reason_to_stop = ValueObject::eExpressionPathScanEndReasonEndOfString;
2778             return root;
2779         }
2780 
2781         switch (*expression_cstr)
2782         {
2783             case '-':
2784             {
2785                 if (options.m_check_dot_vs_arrow_syntax &&
2786                     root_compiler_type_info.Test(eTypeIsPointer) ) // if you are trying to use -> on a non-pointer and I must catch the error
2787                 {
2788                     *first_unparsed = expression_cstr;
2789                     *reason_to_stop = ValueObject::eExpressionPathScanEndReasonArrowInsteadOfDot;
2790                     *final_result = ValueObject::eExpressionPathEndResultTypeInvalid;
2791                     return ValueObjectSP();
2792                 }
2793                 if (root_compiler_type_info.Test(eTypeIsObjC) &&  // if yo are trying to extract an ObjC IVar when this is forbidden
2794                     root_compiler_type_info.Test(eTypeIsPointer) &&
2795                     options.m_no_fragile_ivar)
2796                 {
2797                     *first_unparsed = expression_cstr;
2798                     *reason_to_stop = ValueObject::eExpressionPathScanEndReasonFragileIVarNotAllowed;
2799                     *final_result = ValueObject::eExpressionPathEndResultTypeInvalid;
2800                     return ValueObjectSP();
2801                 }
2802                 if (expression_cstr[1] != '>')
2803                 {
2804                     *first_unparsed = expression_cstr;
2805                     *reason_to_stop = ValueObject::eExpressionPathScanEndReasonUnexpectedSymbol;
2806                     *final_result = ValueObject::eExpressionPathEndResultTypeInvalid;
2807                     return ValueObjectSP();
2808                 }
2809                 expression_cstr++; // skip the -
2810             }
2811             case '.': // or fallthrough from ->
2812             {
2813                 if (options.m_check_dot_vs_arrow_syntax && *expression_cstr == '.' &&
2814                     root_compiler_type_info.Test(eTypeIsPointer)) // if you are trying to use . on a pointer and I must catch the error
2815                 {
2816                     *first_unparsed = expression_cstr;
2817                     *reason_to_stop = ValueObject::eExpressionPathScanEndReasonDotInsteadOfArrow;
2818                     *final_result = ValueObject::eExpressionPathEndResultTypeInvalid;
2819                     return ValueObjectSP();
2820                 }
2821                 expression_cstr++; // skip .
2822                 const char *next_separator = strpbrk(expression_cstr+1,"-.[");
2823                 ConstString child_name;
2824                 if (!next_separator) // if no other separator just expand this last layer
2825                 {
2826                     child_name.SetCString (expression_cstr);
2827                     ValueObjectSP child_valobj_sp = root->GetChildMemberWithName(child_name, true);
2828 
2829                     if (child_valobj_sp.get()) // we know we are done, so just return
2830                     {
2831                         *first_unparsed = "";
2832                         *reason_to_stop = ValueObject::eExpressionPathScanEndReasonEndOfString;
2833                         *final_result = ValueObject::eExpressionPathEndResultTypePlain;
2834                         return child_valobj_sp;
2835                     }
2836                     else
2837                     {
2838                         switch (options.m_synthetic_children_traversal)
2839                         {
2840                             case GetValueForExpressionPathOptions::SyntheticChildrenTraversal::None:
2841                                 break;
2842                             case GetValueForExpressionPathOptions::SyntheticChildrenTraversal::FromSynthetic:
2843                                 if (root->IsSynthetic())
2844                                 {
2845                                     child_valobj_sp = root->GetNonSyntheticValue();
2846                                     if (child_valobj_sp.get())
2847                                         child_valobj_sp = child_valobj_sp->GetChildMemberWithName(child_name, true);
2848                                 }
2849                                 break;
2850                             case GetValueForExpressionPathOptions::SyntheticChildrenTraversal::ToSynthetic:
2851                                 if (!root->IsSynthetic())
2852                                 {
2853                                     child_valobj_sp = root->GetSyntheticValue();
2854                                     if (child_valobj_sp.get())
2855                                         child_valobj_sp = child_valobj_sp->GetChildMemberWithName(child_name, true);
2856                                 }
2857                                 break;
2858                             case GetValueForExpressionPathOptions::SyntheticChildrenTraversal::Both:
2859                                 if (root->IsSynthetic())
2860                                 {
2861                                     child_valobj_sp = root->GetNonSyntheticValue();
2862                                     if (child_valobj_sp.get())
2863                                         child_valobj_sp = child_valobj_sp->GetChildMemberWithName(child_name, true);
2864                                 }
2865                                 else
2866                                 {
2867                                     child_valobj_sp = root->GetSyntheticValue();
2868                                     if (child_valobj_sp.get())
2869                                         child_valobj_sp = child_valobj_sp->GetChildMemberWithName(child_name, true);
2870                                 }
2871                                 break;
2872                         }
2873                     }
2874 
2875                     // if we are here and options.m_no_synthetic_children is true, child_valobj_sp is going to be a NULL SP,
2876                     // so we hit the "else" branch, and return an error
2877                     if(child_valobj_sp.get()) // if it worked, just return
2878                     {
2879                         *first_unparsed = "";
2880                         *reason_to_stop = ValueObject::eExpressionPathScanEndReasonEndOfString;
2881                         *final_result = ValueObject::eExpressionPathEndResultTypePlain;
2882                         return child_valobj_sp;
2883                     }
2884                     else
2885                     {
2886                         *first_unparsed = expression_cstr;
2887                         *reason_to_stop = ValueObject::eExpressionPathScanEndReasonNoSuchChild;
2888                         *final_result = ValueObject::eExpressionPathEndResultTypeInvalid;
2889                         return ValueObjectSP();
2890                     }
2891                 }
2892                 else // other layers do expand
2893                 {
2894                     child_name.SetCStringWithLength(expression_cstr, next_separator - expression_cstr);
2895                     ValueObjectSP child_valobj_sp = root->GetChildMemberWithName(child_name, true);
2896                     if (child_valobj_sp.get()) // store the new root and move on
2897                     {
2898                         root = child_valobj_sp;
2899                         *first_unparsed = next_separator;
2900                         *final_result = ValueObject::eExpressionPathEndResultTypePlain;
2901                         continue;
2902                     }
2903                     else
2904                     {
2905                         switch (options.m_synthetic_children_traversal)
2906                         {
2907                             case GetValueForExpressionPathOptions::SyntheticChildrenTraversal::None:
2908                                 break;
2909                             case GetValueForExpressionPathOptions::SyntheticChildrenTraversal::FromSynthetic:
2910                                 if (root->IsSynthetic())
2911                                 {
2912                                     child_valobj_sp = root->GetNonSyntheticValue();
2913                                     if (child_valobj_sp.get())
2914                                         child_valobj_sp = child_valobj_sp->GetChildMemberWithName(child_name, true);
2915                                 }
2916                                 break;
2917                             case GetValueForExpressionPathOptions::SyntheticChildrenTraversal::ToSynthetic:
2918                                 if (!root->IsSynthetic())
2919                                 {
2920                                     child_valobj_sp = root->GetSyntheticValue();
2921                                     if (child_valobj_sp.get())
2922                                         child_valobj_sp = child_valobj_sp->GetChildMemberWithName(child_name, true);
2923                                 }
2924                                 break;
2925                             case GetValueForExpressionPathOptions::SyntheticChildrenTraversal::Both:
2926                                 if (root->IsSynthetic())
2927                                 {
2928                                     child_valobj_sp = root->GetNonSyntheticValue();
2929                                     if (child_valobj_sp.get())
2930                                         child_valobj_sp = child_valobj_sp->GetChildMemberWithName(child_name, true);
2931                                 }
2932                                 else
2933                                 {
2934                                     child_valobj_sp = root->GetSyntheticValue();
2935                                     if (child_valobj_sp.get())
2936                                         child_valobj_sp = child_valobj_sp->GetChildMemberWithName(child_name, true);
2937                                 }
2938                                 break;
2939                         }
2940                     }
2941 
2942                     // if we are here and options.m_no_synthetic_children is true, child_valobj_sp is going to be a NULL SP,
2943                     // so we hit the "else" branch, and return an error
2944                     if(child_valobj_sp.get()) // if it worked, move on
2945                     {
2946                         root = child_valobj_sp;
2947                         *first_unparsed = next_separator;
2948                         *final_result = ValueObject::eExpressionPathEndResultTypePlain;
2949                         continue;
2950                     }
2951                     else
2952                     {
2953                         *first_unparsed = expression_cstr;
2954                         *reason_to_stop = ValueObject::eExpressionPathScanEndReasonNoSuchChild;
2955                         *final_result = ValueObject::eExpressionPathEndResultTypeInvalid;
2956                         return ValueObjectSP();
2957                     }
2958                 }
2959                 break;
2960             }
2961             case '[':
2962             {
2963                 if (!root_compiler_type_info.Test(eTypeIsArray) && !root_compiler_type_info.Test(eTypeIsPointer) && !root_compiler_type_info.Test(eTypeIsVector)) // if this is not a T[] nor a T*
2964                 {
2965                     if (!root_compiler_type_info.Test(eTypeIsScalar)) // if this is not even a scalar...
2966                     {
2967                         if (options.m_synthetic_children_traversal == GetValueForExpressionPathOptions::SyntheticChildrenTraversal::None) // ...only chance left is synthetic
2968                         {
2969                             *first_unparsed = expression_cstr;
2970                             *reason_to_stop = ValueObject::eExpressionPathScanEndReasonRangeOperatorInvalid;
2971                             *final_result = ValueObject::eExpressionPathEndResultTypeInvalid;
2972                             return ValueObjectSP();
2973                         }
2974                     }
2975                     else if (!options.m_allow_bitfields_syntax) // if this is a scalar, check that we can expand bitfields
2976                     {
2977                         *first_unparsed = expression_cstr;
2978                         *reason_to_stop = ValueObject::eExpressionPathScanEndReasonRangeOperatorNotAllowed;
2979                         *final_result = ValueObject::eExpressionPathEndResultTypeInvalid;
2980                         return ValueObjectSP();
2981                     }
2982                 }
2983                 if (*(expression_cstr+1) == ']') // if this is an unbounded range it only works for arrays
2984                 {
2985                     if (!root_compiler_type_info.Test(eTypeIsArray))
2986                     {
2987                         *first_unparsed = expression_cstr;
2988                         *reason_to_stop = ValueObject::eExpressionPathScanEndReasonEmptyRangeNotAllowed;
2989                         *final_result = ValueObject::eExpressionPathEndResultTypeInvalid;
2990                         return ValueObjectSP();
2991                     }
2992                     else // even if something follows, we cannot expand unbounded ranges, just let the caller do it
2993                     {
2994                         *first_unparsed = expression_cstr+2;
2995                         *reason_to_stop = ValueObject::eExpressionPathScanEndReasonArrayRangeOperatorMet;
2996                         *final_result = ValueObject::eExpressionPathEndResultTypeUnboundedRange;
2997                         return root;
2998                     }
2999                 }
3000                 const char *separator_position = ::strchr(expression_cstr+1,'-');
3001                 const char *close_bracket_position = ::strchr(expression_cstr+1,']');
3002                 if (!close_bracket_position) // if there is no ], this is a syntax error
3003                 {
3004                     *first_unparsed = expression_cstr;
3005                     *reason_to_stop = ValueObject::eExpressionPathScanEndReasonUnexpectedSymbol;
3006                     *final_result = ValueObject::eExpressionPathEndResultTypeInvalid;
3007                     return ValueObjectSP();
3008                 }
3009                 if (!separator_position || separator_position > close_bracket_position) // if no separator, this is either [] or [N]
3010                 {
3011                     char *end = NULL;
3012                     unsigned long index = ::strtoul (expression_cstr+1, &end, 0);
3013                     if (!end || end != close_bracket_position) // if something weird is in our way return an error
3014                     {
3015                         *first_unparsed = expression_cstr;
3016                         *reason_to_stop = ValueObject::eExpressionPathScanEndReasonUnexpectedSymbol;
3017                         *final_result = ValueObject::eExpressionPathEndResultTypeInvalid;
3018                         return ValueObjectSP();
3019                     }
3020                     if (end - expression_cstr == 1) // if this is [], only return a valid value for arrays
3021                     {
3022                         if (root_compiler_type_info.Test(eTypeIsArray))
3023                         {
3024                             *first_unparsed = expression_cstr+2;
3025                             *reason_to_stop = ValueObject::eExpressionPathScanEndReasonArrayRangeOperatorMet;
3026                             *final_result = ValueObject::eExpressionPathEndResultTypeUnboundedRange;
3027                             return root;
3028                         }
3029                         else
3030                         {
3031                             *first_unparsed = expression_cstr;
3032                             *reason_to_stop = ValueObject::eExpressionPathScanEndReasonEmptyRangeNotAllowed;
3033                             *final_result = ValueObject::eExpressionPathEndResultTypeInvalid;
3034                             return ValueObjectSP();
3035                         }
3036                     }
3037                     // from here on we do have a valid index
3038                     if (root_compiler_type_info.Test(eTypeIsArray))
3039                     {
3040                         ValueObjectSP child_valobj_sp = root->GetChildAtIndex(index, true);
3041                         if (!child_valobj_sp)
3042                             child_valobj_sp = root->GetSyntheticArrayMember(index, true);
3043                         if (!child_valobj_sp)
3044                             if (root->HasSyntheticValue() && root->GetSyntheticValue()->GetNumChildren() > index)
3045                                 child_valobj_sp = root->GetSyntheticValue()->GetChildAtIndex(index, true);
3046                         if (child_valobj_sp)
3047                         {
3048                             root = child_valobj_sp;
3049                             *first_unparsed = end+1; // skip ]
3050                             *final_result = ValueObject::eExpressionPathEndResultTypePlain;
3051                             continue;
3052                         }
3053                         else
3054                         {
3055                             *first_unparsed = expression_cstr;
3056                             *reason_to_stop = ValueObject::eExpressionPathScanEndReasonNoSuchChild;
3057                             *final_result = ValueObject::eExpressionPathEndResultTypeInvalid;
3058                             return ValueObjectSP();
3059                         }
3060                     }
3061                     else if (root_compiler_type_info.Test(eTypeIsPointer))
3062                     {
3063                         if (*what_next == ValueObject::eExpressionPathAftermathDereference &&  // 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
3064                             pointee_compiler_type_info.Test(eTypeIsScalar))
3065                         {
3066                             Error error;
3067                             root = root->Dereference(error);
3068                             if (error.Fail() || !root.get())
3069                             {
3070                                 *first_unparsed = expression_cstr;
3071                                 *reason_to_stop = ValueObject::eExpressionPathScanEndReasonDereferencingFailed;
3072                                 *final_result = ValueObject::eExpressionPathEndResultTypeInvalid;
3073                                 return ValueObjectSP();
3074                             }
3075                             else
3076                             {
3077                                 *what_next = eExpressionPathAftermathNothing;
3078                                 continue;
3079                             }
3080                         }
3081                         else
3082                         {
3083                             if (root->GetCompilerType().GetMinimumLanguage() == eLanguageTypeObjC
3084                                 && pointee_compiler_type_info.AllClear(eTypeIsPointer)
3085                                 && root->HasSyntheticValue()
3086                                 && (options.m_synthetic_children_traversal == GetValueForExpressionPathOptions::SyntheticChildrenTraversal::ToSynthetic ||
3087                                     options.m_synthetic_children_traversal == GetValueForExpressionPathOptions::SyntheticChildrenTraversal::Both))
3088                             {
3089                                 root = root->GetSyntheticValue()->GetChildAtIndex(index, true);
3090                             }
3091                             else
3092                                 root = root->GetSyntheticArrayMember(index, true);
3093                             if (!root.get())
3094                             {
3095                                 *first_unparsed = expression_cstr;
3096                                 *reason_to_stop = ValueObject::eExpressionPathScanEndReasonNoSuchChild;
3097                                 *final_result = ValueObject::eExpressionPathEndResultTypeInvalid;
3098                                 return ValueObjectSP();
3099                             }
3100                             else
3101                             {
3102                                 *first_unparsed = end+1; // skip ]
3103                                 *final_result = ValueObject::eExpressionPathEndResultTypePlain;
3104                                 continue;
3105                             }
3106                         }
3107                     }
3108                     else if (root_compiler_type_info.Test(eTypeIsScalar))
3109                     {
3110                         root = root->GetSyntheticBitFieldChild(index, index, true);
3111                         if (!root.get())
3112                         {
3113                             *first_unparsed = expression_cstr;
3114                             *reason_to_stop = ValueObject::eExpressionPathScanEndReasonNoSuchChild;
3115                             *final_result = ValueObject::eExpressionPathEndResultTypeInvalid;
3116                             return ValueObjectSP();
3117                         }
3118                         else // we do not know how to expand members of bitfields, so we just return and let the caller do any further processing
3119                         {
3120                             *first_unparsed = end+1; // skip ]
3121                             *reason_to_stop = ValueObject::eExpressionPathScanEndReasonBitfieldRangeOperatorMet;
3122                             *final_result = ValueObject::eExpressionPathEndResultTypeBitfield;
3123                             return root;
3124                         }
3125                     }
3126                     else if (root_compiler_type_info.Test(eTypeIsVector))
3127                     {
3128                         root = root->GetChildAtIndex(index, true);
3129                         if (!root.get())
3130                         {
3131                             *first_unparsed = expression_cstr;
3132                             *reason_to_stop = ValueObject::eExpressionPathScanEndReasonNoSuchChild;
3133                             *final_result = ValueObject::eExpressionPathEndResultTypeInvalid;
3134                             return ValueObjectSP();
3135                         }
3136                         else
3137                         {
3138                             *first_unparsed = end+1; // skip ]
3139                             *final_result = ValueObject::eExpressionPathEndResultTypePlain;
3140                             continue;
3141                         }
3142                     }
3143                     else if (options.m_synthetic_children_traversal == GetValueForExpressionPathOptions::SyntheticChildrenTraversal::ToSynthetic ||
3144                              options.m_synthetic_children_traversal == GetValueForExpressionPathOptions::SyntheticChildrenTraversal::Both)
3145                     {
3146                         if (root->HasSyntheticValue())
3147                             root = root->GetSyntheticValue();
3148                         else if (!root->IsSynthetic())
3149                         {
3150                             *first_unparsed = expression_cstr;
3151                             *reason_to_stop = ValueObject::eExpressionPathScanEndReasonSyntheticValueMissing;
3152                             *final_result = ValueObject::eExpressionPathEndResultTypeInvalid;
3153                             return ValueObjectSP();
3154                         }
3155                         // if we are here, then root itself is a synthetic VO.. should be good to go
3156 
3157                         if (!root.get())
3158                         {
3159                             *first_unparsed = expression_cstr;
3160                             *reason_to_stop = ValueObject::eExpressionPathScanEndReasonSyntheticValueMissing;
3161                             *final_result = ValueObject::eExpressionPathEndResultTypeInvalid;
3162                             return ValueObjectSP();
3163                         }
3164                         root = root->GetChildAtIndex(index, true);
3165                         if (!root.get())
3166                         {
3167                             *first_unparsed = expression_cstr;
3168                             *reason_to_stop = ValueObject::eExpressionPathScanEndReasonNoSuchChild;
3169                             *final_result = ValueObject::eExpressionPathEndResultTypeInvalid;
3170                             return ValueObjectSP();
3171                         }
3172                         else
3173                         {
3174                             *first_unparsed = end+1; // skip ]
3175                             *final_result = ValueObject::eExpressionPathEndResultTypePlain;
3176                             continue;
3177                         }
3178                     }
3179                     else
3180                     {
3181                         *first_unparsed = expression_cstr;
3182                         *reason_to_stop = ValueObject::eExpressionPathScanEndReasonNoSuchChild;
3183                         *final_result = ValueObject::eExpressionPathEndResultTypeInvalid;
3184                         return ValueObjectSP();
3185                     }
3186                 }
3187                 else // we have a low and a high index
3188                 {
3189                     char *end = NULL;
3190                     unsigned long index_lower = ::strtoul (expression_cstr+1, &end, 0);
3191                     if (!end || end != separator_position) // if something weird is in our way return an error
3192                     {
3193                         *first_unparsed = expression_cstr;
3194                         *reason_to_stop = ValueObject::eExpressionPathScanEndReasonUnexpectedSymbol;
3195                         *final_result = ValueObject::eExpressionPathEndResultTypeInvalid;
3196                         return ValueObjectSP();
3197                     }
3198                     unsigned long index_higher = ::strtoul (separator_position+1, &end, 0);
3199                     if (!end || end != close_bracket_position) // if something weird is in our way return an error
3200                     {
3201                         *first_unparsed = expression_cstr;
3202                         *reason_to_stop = ValueObject::eExpressionPathScanEndReasonUnexpectedSymbol;
3203                         *final_result = ValueObject::eExpressionPathEndResultTypeInvalid;
3204                         return ValueObjectSP();
3205                     }
3206                     if (index_lower > index_higher) // swap indices if required
3207                     {
3208                         unsigned long temp = index_lower;
3209                         index_lower = index_higher;
3210                         index_higher = temp;
3211                     }
3212                     if (root_compiler_type_info.Test(eTypeIsScalar)) // expansion only works for scalars
3213                     {
3214                         root = root->GetSyntheticBitFieldChild(index_lower, index_higher, true);
3215                         if (!root.get())
3216                         {
3217                             *first_unparsed = expression_cstr;
3218                             *reason_to_stop = ValueObject::eExpressionPathScanEndReasonNoSuchChild;
3219                             *final_result = ValueObject::eExpressionPathEndResultTypeInvalid;
3220                             return ValueObjectSP();
3221                         }
3222                         else
3223                         {
3224                             *first_unparsed = end+1; // skip ]
3225                             *reason_to_stop = ValueObject::eExpressionPathScanEndReasonBitfieldRangeOperatorMet;
3226                             *final_result = ValueObject::eExpressionPathEndResultTypeBitfield;
3227                             return root;
3228                         }
3229                     }
3230                     else if (root_compiler_type_info.Test(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
3231                              *what_next == ValueObject::eExpressionPathAftermathDereference &&
3232                              pointee_compiler_type_info.Test(eTypeIsScalar))
3233                     {
3234                         Error error;
3235                         root = root->Dereference(error);
3236                         if (error.Fail() || !root.get())
3237                         {
3238                             *first_unparsed = expression_cstr;
3239                             *reason_to_stop = ValueObject::eExpressionPathScanEndReasonDereferencingFailed;
3240                             *final_result = ValueObject::eExpressionPathEndResultTypeInvalid;
3241                             return ValueObjectSP();
3242                         }
3243                         else
3244                         {
3245                             *what_next = ValueObject::eExpressionPathAftermathNothing;
3246                             continue;
3247                         }
3248                     }
3249                     else
3250                     {
3251                         *first_unparsed = expression_cstr;
3252                         *reason_to_stop = ValueObject::eExpressionPathScanEndReasonArrayRangeOperatorMet;
3253                         *final_result = ValueObject::eExpressionPathEndResultTypeBoundedRange;
3254                         return root;
3255                     }
3256                 }
3257                 break;
3258             }
3259             default: // some non-separator is in the way
3260             {
3261                 *first_unparsed = expression_cstr;
3262                 *reason_to_stop = ValueObject::eExpressionPathScanEndReasonUnexpectedSymbol;
3263                 *final_result = ValueObject::eExpressionPathEndResultTypeInvalid;
3264                 return ValueObjectSP();
3265                 break;
3266             }
3267         }
3268     }
3269 }
3270 
3271 int
3272 ValueObject::ExpandArraySliceExpression(const char* expression_cstr,
3273                                         const char** first_unparsed,
3274                                         ValueObjectSP root,
3275                                         ValueObjectListSP& list,
3276                                         ExpressionPathScanEndReason* reason_to_stop,
3277                                         ExpressionPathEndResultType* final_result,
3278                                         const GetValueForExpressionPathOptions& options,
3279                                         ExpressionPathAftermath* what_next)
3280 {
3281     if (!root.get())
3282         return 0;
3283 
3284     *first_unparsed = expression_cstr;
3285 
3286     while (true)
3287     {
3288 
3289         const char* expression_cstr = *first_unparsed; // hide the top level expression_cstr
3290 
3291         CompilerType root_compiler_type = root->GetCompilerType();
3292         CompilerType pointee_compiler_type;
3293         Flags pointee_compiler_type_info;
3294         Flags root_compiler_type_info(root_compiler_type.GetTypeInfo(&pointee_compiler_type));
3295         if (pointee_compiler_type)
3296             pointee_compiler_type_info.Reset(pointee_compiler_type.GetTypeInfo());
3297 
3298         if (!expression_cstr || *expression_cstr == '\0')
3299         {
3300             *reason_to_stop = ValueObject::eExpressionPathScanEndReasonEndOfString;
3301             list->Append(root);
3302             return 1;
3303         }
3304 
3305         switch (*expression_cstr)
3306         {
3307             case '[':
3308             {
3309                 if (!root_compiler_type_info.Test(eTypeIsArray) && !root_compiler_type_info.Test(eTypeIsPointer)) // if this is not a T[] nor a T*
3310                 {
3311                     if (!root_compiler_type_info.Test(eTypeIsScalar)) // if this is not even a scalar, this syntax is just plain wrong!
3312                     {
3313                         *first_unparsed = expression_cstr;
3314                         *reason_to_stop = ValueObject::eExpressionPathScanEndReasonRangeOperatorInvalid;
3315                         *final_result = ValueObject::eExpressionPathEndResultTypeInvalid;
3316                         return 0;
3317                     }
3318                     else if (!options.m_allow_bitfields_syntax) // if this is a scalar, check that we can expand bitfields
3319                     {
3320                         *first_unparsed = expression_cstr;
3321                         *reason_to_stop = ValueObject::eExpressionPathScanEndReasonRangeOperatorNotAllowed;
3322                         *final_result = ValueObject::eExpressionPathEndResultTypeInvalid;
3323                         return 0;
3324                     }
3325                 }
3326                 if (*(expression_cstr+1) == ']') // if this is an unbounded range it only works for arrays
3327                 {
3328                     if (!root_compiler_type_info.Test(eTypeIsArray))
3329                     {
3330                         *first_unparsed = expression_cstr;
3331                         *reason_to_stop = ValueObject::eExpressionPathScanEndReasonEmptyRangeNotAllowed;
3332                         *final_result = ValueObject::eExpressionPathEndResultTypeInvalid;
3333                         return 0;
3334                     }
3335                     else // expand this into list
3336                     {
3337                         const size_t max_index = root->GetNumChildren() - 1;
3338                         for (size_t index = 0; index < max_index; index++)
3339                         {
3340                             ValueObjectSP child =
3341                                 root->GetChildAtIndex(index, true);
3342                             list->Append(child);
3343                         }
3344                         *first_unparsed = expression_cstr+2;
3345                         *reason_to_stop = ValueObject::eExpressionPathScanEndReasonRangeOperatorExpanded;
3346                         *final_result = ValueObject::eExpressionPathEndResultTypeValueObjectList;
3347                         return max_index; // tell me number of items I added to the VOList
3348                     }
3349                 }
3350                 const char *separator_position = ::strchr(expression_cstr+1,'-');
3351                 const char *close_bracket_position = ::strchr(expression_cstr+1,']');
3352                 if (!close_bracket_position) // if there is no ], this is a syntax error
3353                 {
3354                     *first_unparsed = expression_cstr;
3355                     *reason_to_stop = ValueObject::eExpressionPathScanEndReasonUnexpectedSymbol;
3356                     *final_result = ValueObject::eExpressionPathEndResultTypeInvalid;
3357                     return 0;
3358                 }
3359                 if (!separator_position || separator_position > close_bracket_position) // if no separator, this is either [] or [N]
3360                 {
3361                     char *end = NULL;
3362                     unsigned long index = ::strtoul (expression_cstr+1, &end, 0);
3363                     if (!end || end != close_bracket_position) // if something weird is in our way return an error
3364                     {
3365                         *first_unparsed = expression_cstr;
3366                         *reason_to_stop = ValueObject::eExpressionPathScanEndReasonUnexpectedSymbol;
3367                         *final_result = ValueObject::eExpressionPathEndResultTypeInvalid;
3368                         return 0;
3369                     }
3370                     if (end - expression_cstr == 1) // if this is [], only return a valid value for arrays
3371                     {
3372                         if (root_compiler_type_info.Test(eTypeIsArray))
3373                         {
3374                             const size_t max_index = root->GetNumChildren() - 1;
3375                             for (size_t index = 0; index < max_index; index++)
3376                             {
3377                                 ValueObjectSP child =
3378                                 root->GetChildAtIndex(index, true);
3379                                 list->Append(child);
3380                             }
3381                             *first_unparsed = expression_cstr+2;
3382                             *reason_to_stop = ValueObject::eExpressionPathScanEndReasonRangeOperatorExpanded;
3383                             *final_result = ValueObject::eExpressionPathEndResultTypeValueObjectList;
3384                             return max_index; // tell me number of items I added to the VOList
3385                         }
3386                         else
3387                         {
3388                             *first_unparsed = expression_cstr;
3389                             *reason_to_stop = ValueObject::eExpressionPathScanEndReasonEmptyRangeNotAllowed;
3390                             *final_result = ValueObject::eExpressionPathEndResultTypeInvalid;
3391                             return 0;
3392                         }
3393                     }
3394                     // from here on we do have a valid index
3395                     if (root_compiler_type_info.Test(eTypeIsArray))
3396                     {
3397                         root = root->GetChildAtIndex(index, true);
3398                         if (!root.get())
3399                         {
3400                             *first_unparsed = expression_cstr;
3401                             *reason_to_stop = ValueObject::eExpressionPathScanEndReasonNoSuchChild;
3402                             *final_result = ValueObject::eExpressionPathEndResultTypeInvalid;
3403                             return 0;
3404                         }
3405                         else
3406                         {
3407                             list->Append(root);
3408                             *first_unparsed = end+1; // skip ]
3409                             *reason_to_stop = ValueObject::eExpressionPathScanEndReasonRangeOperatorExpanded;
3410                             *final_result = ValueObject::eExpressionPathEndResultTypeValueObjectList;
3411                             return 1;
3412                         }
3413                     }
3414                     else if (root_compiler_type_info.Test(eTypeIsPointer))
3415                     {
3416                         if (*what_next == ValueObject::eExpressionPathAftermathDereference &&  // 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
3417                             pointee_compiler_type_info.Test(eTypeIsScalar))
3418                         {
3419                             Error error;
3420                             root = root->Dereference(error);
3421                             if (error.Fail() || !root.get())
3422                             {
3423                                 *first_unparsed = expression_cstr;
3424                                 *reason_to_stop = ValueObject::eExpressionPathScanEndReasonDereferencingFailed;
3425                                 *final_result = ValueObject::eExpressionPathEndResultTypeInvalid;
3426                                 return 0;
3427                             }
3428                             else
3429                             {
3430                                 *what_next = eExpressionPathAftermathNothing;
3431                                 continue;
3432                             }
3433                         }
3434                         else
3435                         {
3436                             root = root->GetSyntheticArrayMember(index, true);
3437                             if (!root.get())
3438                             {
3439                                 *first_unparsed = expression_cstr;
3440                                 *reason_to_stop = ValueObject::eExpressionPathScanEndReasonNoSuchChild;
3441                                 *final_result = ValueObject::eExpressionPathEndResultTypeInvalid;
3442                                 return 0;
3443                             }
3444                             else
3445                             {
3446                                 list->Append(root);
3447                                 *first_unparsed = end+1; // skip ]
3448                                 *reason_to_stop = ValueObject::eExpressionPathScanEndReasonRangeOperatorExpanded;
3449                                 *final_result = ValueObject::eExpressionPathEndResultTypeValueObjectList;
3450                                 return 1;
3451                             }
3452                         }
3453                     }
3454                     else /*if (ClangASTContext::IsScalarType(root_compiler_type))*/
3455                     {
3456                         root = root->GetSyntheticBitFieldChild(index, index, true);
3457                         if (!root.get())
3458                         {
3459                             *first_unparsed = expression_cstr;
3460                             *reason_to_stop = ValueObject::eExpressionPathScanEndReasonNoSuchChild;
3461                             *final_result = ValueObject::eExpressionPathEndResultTypeInvalid;
3462                             return 0;
3463                         }
3464                         else // we do not know how to expand members of bitfields, so we just return and let the caller do any further processing
3465                         {
3466                             list->Append(root);
3467                             *first_unparsed = end+1; // skip ]
3468                             *reason_to_stop = ValueObject::eExpressionPathScanEndReasonRangeOperatorExpanded;
3469                             *final_result = ValueObject::eExpressionPathEndResultTypeValueObjectList;
3470                             return 1;
3471                         }
3472                     }
3473                 }
3474                 else // we have a low and a high index
3475                 {
3476                     char *end = NULL;
3477                     unsigned long index_lower = ::strtoul (expression_cstr+1, &end, 0);
3478                     if (!end || end != separator_position) // if something weird is in our way return an error
3479                     {
3480                         *first_unparsed = expression_cstr;
3481                         *reason_to_stop = ValueObject::eExpressionPathScanEndReasonUnexpectedSymbol;
3482                         *final_result = ValueObject::eExpressionPathEndResultTypeInvalid;
3483                         return 0;
3484                     }
3485                     unsigned long index_higher = ::strtoul (separator_position+1, &end, 0);
3486                     if (!end || end != close_bracket_position) // if something weird is in our way return an error
3487                     {
3488                         *first_unparsed = expression_cstr;
3489                         *reason_to_stop = ValueObject::eExpressionPathScanEndReasonUnexpectedSymbol;
3490                         *final_result = ValueObject::eExpressionPathEndResultTypeInvalid;
3491                         return 0;
3492                     }
3493                     if (index_lower > index_higher) // swap indices if required
3494                     {
3495                         unsigned long temp = index_lower;
3496                         index_lower = index_higher;
3497                         index_higher = temp;
3498                     }
3499                     if (root_compiler_type_info.Test(eTypeIsScalar)) // expansion only works for scalars
3500                     {
3501                         root = root->GetSyntheticBitFieldChild(index_lower, index_higher, true);
3502                         if (!root.get())
3503                         {
3504                             *first_unparsed = expression_cstr;
3505                             *reason_to_stop = ValueObject::eExpressionPathScanEndReasonNoSuchChild;
3506                             *final_result = ValueObject::eExpressionPathEndResultTypeInvalid;
3507                             return 0;
3508                         }
3509                         else
3510                         {
3511                             list->Append(root);
3512                             *first_unparsed = end+1; // skip ]
3513                             *reason_to_stop = ValueObject::eExpressionPathScanEndReasonRangeOperatorExpanded;
3514                             *final_result = ValueObject::eExpressionPathEndResultTypeValueObjectList;
3515                             return 1;
3516                         }
3517                     }
3518                     else if (root_compiler_type_info.Test(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
3519                              *what_next == ValueObject::eExpressionPathAftermathDereference &&
3520                              pointee_compiler_type_info.Test(eTypeIsScalar))
3521                     {
3522                         Error error;
3523                         root = root->Dereference(error);
3524                         if (error.Fail() || !root.get())
3525                         {
3526                             *first_unparsed = expression_cstr;
3527                             *reason_to_stop = ValueObject::eExpressionPathScanEndReasonDereferencingFailed;
3528                             *final_result = ValueObject::eExpressionPathEndResultTypeInvalid;
3529                             return 0;
3530                         }
3531                         else
3532                         {
3533                             *what_next = ValueObject::eExpressionPathAftermathNothing;
3534                             continue;
3535                         }
3536                     }
3537                     else
3538                     {
3539                         for (unsigned long index = index_lower;
3540                              index <= index_higher; index++)
3541                         {
3542                             ValueObjectSP child =
3543                                 root->GetChildAtIndex(index, true);
3544                             list->Append(child);
3545                         }
3546                         *first_unparsed = end+1;
3547                         *reason_to_stop = ValueObject::eExpressionPathScanEndReasonRangeOperatorExpanded;
3548                         *final_result = ValueObject::eExpressionPathEndResultTypeValueObjectList;
3549                         return index_higher-index_lower+1; // tell me number of items I added to the VOList
3550                     }
3551                 }
3552                 break;
3553             }
3554             default: // some non-[ separator, or something entirely wrong, is in the way
3555             {
3556                 *first_unparsed = expression_cstr;
3557                 *reason_to_stop = ValueObject::eExpressionPathScanEndReasonUnexpectedSymbol;
3558                 *final_result = ValueObject::eExpressionPathEndResultTypeInvalid;
3559                 return 0;
3560                 break;
3561             }
3562         }
3563     }
3564 }
3565 
3566 void
3567 ValueObject::LogValueObject (Log *log)
3568 {
3569     if (log)
3570         return LogValueObject (log, DumpValueObjectOptions(*this));
3571 }
3572 
3573 void
3574 ValueObject::LogValueObject (Log *log, const DumpValueObjectOptions& options)
3575 {
3576     if (log)
3577     {
3578         StreamString s;
3579         Dump (s, options);
3580         if (s.GetSize())
3581             log->PutCString(s.GetData());
3582     }
3583 }
3584 
3585 void
3586 ValueObject::Dump (Stream &s)
3587 {
3588     Dump (s, DumpValueObjectOptions(*this));
3589 }
3590 
3591 void
3592 ValueObject::Dump (Stream &s,
3593                    const DumpValueObjectOptions& options)
3594 {
3595     ValueObjectPrinter printer(this,&s,options);
3596     printer.PrintValueObject();
3597 }
3598 
3599 ValueObjectSP
3600 ValueObject::CreateConstantValue (const ConstString &name)
3601 {
3602     ValueObjectSP valobj_sp;
3603 
3604     if (UpdateValueIfNeeded(false) && m_error.Success())
3605     {
3606         ExecutionContext exe_ctx (GetExecutionContextRef());
3607 
3608         DataExtractor data;
3609         data.SetByteOrder (m_data.GetByteOrder());
3610         data.SetAddressByteSize(m_data.GetAddressByteSize());
3611 
3612         if (IsBitfield())
3613         {
3614             Value v(Scalar(GetValueAsUnsigned(UINT64_MAX)));
3615             m_error = v.GetValueAsData (&exe_ctx, data, 0, GetModule().get());
3616         }
3617         else
3618             m_error = m_value.GetValueAsData (&exe_ctx, data, 0, GetModule().get());
3619 
3620         valobj_sp = ValueObjectConstResult::Create (exe_ctx.GetBestExecutionContextScope(),
3621                                                     GetCompilerType(),
3622                                                     name,
3623                                                     data,
3624                                                     GetAddressOf());
3625     }
3626 
3627     if (!valobj_sp)
3628     {
3629         ExecutionContext exe_ctx (GetExecutionContextRef());
3630         valobj_sp = ValueObjectConstResult::Create (exe_ctx.GetBestExecutionContextScope(), m_error);
3631     }
3632     return valobj_sp;
3633 }
3634 
3635 ValueObjectSP
3636 ValueObject::GetQualifiedRepresentationIfAvailable (lldb::DynamicValueType dynValue,
3637                                                     bool synthValue)
3638 {
3639     ValueObjectSP result_sp(GetSP());
3640 
3641     switch (dynValue)
3642     {
3643         case lldb::eDynamicCanRunTarget:
3644         case lldb::eDynamicDontRunTarget:
3645         {
3646             if (!result_sp->IsDynamic())
3647             {
3648                 if (result_sp->GetDynamicValue(dynValue))
3649                     result_sp = result_sp->GetDynamicValue(dynValue);
3650             }
3651         }
3652             break;
3653         case lldb::eNoDynamicValues:
3654         {
3655             if (result_sp->IsDynamic())
3656             {
3657                 if (result_sp->GetStaticValue())
3658                     result_sp = result_sp->GetStaticValue();
3659             }
3660         }
3661             break;
3662     }
3663 
3664     if (synthValue)
3665     {
3666         if (!result_sp->IsSynthetic())
3667         {
3668             if (result_sp->GetSyntheticValue())
3669                 result_sp = result_sp->GetSyntheticValue();
3670         }
3671     }
3672     else
3673     {
3674         if (result_sp->IsSynthetic())
3675         {
3676             if (result_sp->GetNonSyntheticValue())
3677                 result_sp = result_sp->GetNonSyntheticValue();
3678         }
3679     }
3680 
3681     return result_sp;
3682 }
3683 
3684 lldb::addr_t
3685 ValueObject::GetCPPVTableAddress (AddressType &address_type)
3686 {
3687     CompilerType pointee_type;
3688     CompilerType this_type(GetCompilerType());
3689     uint32_t type_info = this_type.GetTypeInfo(&pointee_type);
3690     if (type_info)
3691     {
3692         bool ptr_or_ref = false;
3693         if (type_info & (eTypeIsPointer | eTypeIsReference))
3694         {
3695             ptr_or_ref = true;
3696             type_info = pointee_type.GetTypeInfo();
3697         }
3698 
3699         const uint32_t cpp_class = eTypeIsClass | eTypeIsCPlusPlus;
3700         if ((type_info & cpp_class) == cpp_class)
3701         {
3702             if (ptr_or_ref)
3703             {
3704                 address_type = GetAddressTypeOfChildren();
3705                 return GetValueAsUnsigned(LLDB_INVALID_ADDRESS);
3706             }
3707             else
3708                 return GetAddressOf (false, &address_type);
3709         }
3710     }
3711 
3712     address_type = eAddressTypeInvalid;
3713     return LLDB_INVALID_ADDRESS;
3714 }
3715 
3716 ValueObjectSP
3717 ValueObject::Dereference (Error &error)
3718 {
3719     if (m_deref_valobj)
3720         return m_deref_valobj->GetSP();
3721 
3722     const bool is_pointer_or_reference_type = IsPointerOrReferenceType();
3723     if (is_pointer_or_reference_type)
3724     {
3725         bool omit_empty_base_classes = true;
3726         bool ignore_array_bounds = false;
3727 
3728         std::string child_name_str;
3729         uint32_t child_byte_size = 0;
3730         int32_t child_byte_offset = 0;
3731         uint32_t child_bitfield_bit_size = 0;
3732         uint32_t child_bitfield_bit_offset = 0;
3733         bool child_is_base_class = false;
3734         bool child_is_deref_of_parent = false;
3735         const bool transparent_pointers = false;
3736         CompilerType compiler_type = GetCompilerType();
3737         CompilerType child_compiler_type;
3738 
3739         ExecutionContext exe_ctx (GetExecutionContextRef());
3740 
3741         child_compiler_type = compiler_type.GetChildCompilerTypeAtIndex (&exe_ctx,
3742                                                                          0,
3743                                                                          transparent_pointers,
3744                                                                          omit_empty_base_classes,
3745                                                                          ignore_array_bounds,
3746                                                                          child_name_str,
3747                                                                          child_byte_size,
3748                                                                          child_byte_offset,
3749                                                                          child_bitfield_bit_size,
3750                                                                          child_bitfield_bit_offset,
3751                                                                          child_is_base_class,
3752                                                                          child_is_deref_of_parent,
3753                                                                          this);
3754         if (child_compiler_type && child_byte_size)
3755         {
3756             ConstString child_name;
3757             if (!child_name_str.empty())
3758                 child_name.SetCString (child_name_str.c_str());
3759 
3760             m_deref_valobj = new ValueObjectChild (*this,
3761                                                    child_compiler_type,
3762                                                    child_name,
3763                                                    child_byte_size,
3764                                                    child_byte_offset,
3765                                                    child_bitfield_bit_size,
3766                                                    child_bitfield_bit_offset,
3767                                                    child_is_base_class,
3768                                                    child_is_deref_of_parent,
3769                                                    eAddressTypeInvalid);
3770         }
3771     }
3772 
3773     if (m_deref_valobj)
3774     {
3775         error.Clear();
3776         return m_deref_valobj->GetSP();
3777     }
3778     else
3779     {
3780         StreamString strm;
3781         GetExpressionPath(strm, true);
3782 
3783         if (is_pointer_or_reference_type)
3784             error.SetErrorStringWithFormat("dereference failed: (%s) %s", GetTypeName().AsCString("<invalid type>"), strm.GetString().c_str());
3785         else
3786             error.SetErrorStringWithFormat("not a pointer or reference type: (%s) %s", GetTypeName().AsCString("<invalid type>"), strm.GetString().c_str());
3787         return ValueObjectSP();
3788     }
3789 }
3790 
3791 ValueObjectSP
3792 ValueObject::AddressOf (Error &error)
3793 {
3794     if (m_addr_of_valobj_sp)
3795         return m_addr_of_valobj_sp;
3796 
3797     AddressType address_type = eAddressTypeInvalid;
3798     const bool scalar_is_load_address = false;
3799     addr_t addr = GetAddressOf (scalar_is_load_address, &address_type);
3800     error.Clear();
3801     if (addr != LLDB_INVALID_ADDRESS && address_type != eAddressTypeHost)
3802     {
3803         switch (address_type)
3804         {
3805         case eAddressTypeInvalid:
3806             {
3807                 StreamString expr_path_strm;
3808                 GetExpressionPath(expr_path_strm, true);
3809                 error.SetErrorStringWithFormat("'%s' is not in memory", expr_path_strm.GetString().c_str());
3810             }
3811             break;
3812 
3813         case eAddressTypeFile:
3814         case eAddressTypeLoad:
3815             {
3816                 CompilerType compiler_type = GetCompilerType();
3817                 if (compiler_type)
3818                 {
3819                     std::string name (1, '&');
3820                     name.append (m_name.AsCString(""));
3821                     ExecutionContext exe_ctx (GetExecutionContextRef());
3822                     m_addr_of_valobj_sp = ValueObjectConstResult::Create (exe_ctx.GetBestExecutionContextScope(),
3823                                                                           compiler_type.GetPointerType(),
3824                                                                           ConstString (name.c_str()),
3825                                                                           addr,
3826                                                                           eAddressTypeInvalid,
3827                                                                           m_data.GetAddressByteSize());
3828                 }
3829             }
3830             break;
3831         default:
3832             break;
3833         }
3834     }
3835     else
3836     {
3837         StreamString expr_path_strm;
3838         GetExpressionPath(expr_path_strm, true);
3839         error.SetErrorStringWithFormat("'%s' doesn't have a valid address", expr_path_strm.GetString().c_str());
3840     }
3841 
3842     return m_addr_of_valobj_sp;
3843 }
3844 
3845 ValueObjectSP
3846 ValueObject::Cast (const CompilerType &compiler_type)
3847 {
3848     return ValueObjectCast::Create (*this, GetName(), compiler_type);
3849 }
3850 
3851 ValueObjectSP
3852 ValueObject::CastPointerType (const char *name, CompilerType &compiler_type)
3853 {
3854     ValueObjectSP valobj_sp;
3855     AddressType address_type;
3856     addr_t ptr_value = GetPointerValue (&address_type);
3857 
3858     if (ptr_value != LLDB_INVALID_ADDRESS)
3859     {
3860         Address ptr_addr (ptr_value);
3861         ExecutionContext exe_ctx (GetExecutionContextRef());
3862         valobj_sp = ValueObjectMemory::Create (exe_ctx.GetBestExecutionContextScope(),
3863                                                name,
3864                                                ptr_addr,
3865                                                compiler_type);
3866     }
3867     return valobj_sp;
3868 }
3869 
3870 ValueObjectSP
3871 ValueObject::CastPointerType (const char *name, TypeSP &type_sp)
3872 {
3873     ValueObjectSP valobj_sp;
3874     AddressType address_type;
3875     addr_t ptr_value = GetPointerValue (&address_type);
3876 
3877     if (ptr_value != LLDB_INVALID_ADDRESS)
3878     {
3879         Address ptr_addr (ptr_value);
3880         ExecutionContext exe_ctx (GetExecutionContextRef());
3881         valobj_sp = ValueObjectMemory::Create (exe_ctx.GetBestExecutionContextScope(),
3882                                                name,
3883                                                ptr_addr,
3884                                                type_sp);
3885     }
3886     return valobj_sp;
3887 }
3888 
3889 ValueObject::EvaluationPoint::EvaluationPoint () :
3890     m_mod_id(),
3891     m_exe_ctx_ref(),
3892     m_needs_update (true)
3893 {
3894 }
3895 
3896 ValueObject::EvaluationPoint::EvaluationPoint (ExecutionContextScope *exe_scope, bool use_selected):
3897     m_mod_id(),
3898     m_exe_ctx_ref(),
3899     m_needs_update (true)
3900 {
3901     ExecutionContext exe_ctx(exe_scope);
3902     TargetSP target_sp (exe_ctx.GetTargetSP());
3903     if (target_sp)
3904     {
3905         m_exe_ctx_ref.SetTargetSP (target_sp);
3906         ProcessSP process_sp (exe_ctx.GetProcessSP());
3907         if (!process_sp)
3908             process_sp = target_sp->GetProcessSP();
3909 
3910         if (process_sp)
3911         {
3912             m_mod_id = process_sp->GetModID();
3913             m_exe_ctx_ref.SetProcessSP (process_sp);
3914 
3915             ThreadSP thread_sp (exe_ctx.GetThreadSP());
3916 
3917             if (!thread_sp)
3918             {
3919                 if (use_selected)
3920                     thread_sp = process_sp->GetThreadList().GetSelectedThread();
3921             }
3922 
3923             if (thread_sp)
3924             {
3925                 m_exe_ctx_ref.SetThreadSP(thread_sp);
3926 
3927                 StackFrameSP frame_sp (exe_ctx.GetFrameSP());
3928                 if (!frame_sp)
3929                 {
3930                     if (use_selected)
3931                         frame_sp = thread_sp->GetSelectedFrame();
3932                 }
3933                 if (frame_sp)
3934                     m_exe_ctx_ref.SetFrameSP(frame_sp);
3935             }
3936         }
3937     }
3938 }
3939 
3940 ValueObject::EvaluationPoint::EvaluationPoint (const ValueObject::EvaluationPoint &rhs) :
3941     m_mod_id(),
3942     m_exe_ctx_ref(rhs.m_exe_ctx_ref),
3943     m_needs_update (true)
3944 {
3945 }
3946 
3947 ValueObject::EvaluationPoint::~EvaluationPoint ()
3948 {
3949 }
3950 
3951 // This function checks the EvaluationPoint against the current process state.  If the current
3952 // state matches the evaluation point, or the evaluation point is already invalid, then we return
3953 // false, meaning "no change".  If the current state is different, we update our state, and return
3954 // true meaning "yes, change".  If we did see a change, we also set m_needs_update to true, so
3955 // future calls to NeedsUpdate will return true.
3956 // exe_scope will be set to the current execution context scope.
3957 
3958 bool
3959 ValueObject::EvaluationPoint::SyncWithProcessState(bool accept_invalid_exe_ctx)
3960 {
3961     // Start with the target, if it is NULL, then we're obviously not going to get any further:
3962     const bool thread_and_frame_only_if_stopped = true;
3963     ExecutionContext exe_ctx(m_exe_ctx_ref.Lock(thread_and_frame_only_if_stopped));
3964 
3965     if (exe_ctx.GetTargetPtr() == NULL)
3966         return false;
3967 
3968     // If we don't have a process nothing can change.
3969     Process *process = exe_ctx.GetProcessPtr();
3970     if (process == NULL)
3971         return false;
3972 
3973     // If our stop id is the current stop ID, nothing has changed:
3974     ProcessModID current_mod_id = process->GetModID();
3975 
3976     // If the current stop id is 0, either we haven't run yet, or the process state has been cleared.
3977     // In either case, we aren't going to be able to sync with the process state.
3978     if (current_mod_id.GetStopID() == 0)
3979         return false;
3980 
3981     bool changed = false;
3982     const bool was_valid = m_mod_id.IsValid();
3983     if (was_valid)
3984     {
3985         if (m_mod_id == current_mod_id)
3986         {
3987             // Everything is already up to date in this object, no need to
3988             // update the execution context scope.
3989             changed = false;
3990         }
3991         else
3992         {
3993             m_mod_id = current_mod_id;
3994             m_needs_update = true;
3995             changed = true;
3996         }
3997     }
3998 
3999     // Now re-look up the thread and frame in case the underlying objects have gone away & been recreated.
4000     // That way we'll be sure to return a valid exe_scope.
4001     // If we used to have a thread or a frame but can't find it anymore, then mark ourselves as invalid.
4002 
4003     if (!accept_invalid_exe_ctx)
4004     {
4005         if (m_exe_ctx_ref.HasThreadRef())
4006         {
4007             ThreadSP thread_sp (m_exe_ctx_ref.GetThreadSP());
4008             if (thread_sp)
4009             {
4010                 if (m_exe_ctx_ref.HasFrameRef())
4011                 {
4012                     StackFrameSP frame_sp (m_exe_ctx_ref.GetFrameSP());
4013                     if (!frame_sp)
4014                     {
4015                         // We used to have a frame, but now it is gone
4016                         SetInvalid();
4017                         changed = was_valid;
4018                     }
4019                 }
4020             }
4021             else
4022             {
4023                 // We used to have a thread, but now it is gone
4024                 SetInvalid();
4025                 changed = was_valid;
4026             }
4027         }
4028     }
4029 
4030     return changed;
4031 }
4032 
4033 void
4034 ValueObject::EvaluationPoint::SetUpdated ()
4035 {
4036     ProcessSP process_sp(m_exe_ctx_ref.GetProcessSP());
4037     if (process_sp)
4038         m_mod_id = process_sp->GetModID();
4039     m_needs_update = false;
4040 }
4041 
4042 
4043 
4044 void
4045 ValueObject::ClearUserVisibleData(uint32_t clear_mask)
4046 {
4047     if ((clear_mask & eClearUserVisibleDataItemsValue) == eClearUserVisibleDataItemsValue)
4048         m_value_str.clear();
4049 
4050     if ((clear_mask & eClearUserVisibleDataItemsLocation) == eClearUserVisibleDataItemsLocation)
4051         m_location_str.clear();
4052 
4053     if ((clear_mask & eClearUserVisibleDataItemsSummary) == eClearUserVisibleDataItemsSummary)
4054         m_summary_str.clear();
4055 
4056     if ((clear_mask & eClearUserVisibleDataItemsDescription) == eClearUserVisibleDataItemsDescription)
4057         m_object_desc_str.clear();
4058 
4059     if ((clear_mask & eClearUserVisibleDataItemsSyntheticChildren) == eClearUserVisibleDataItemsSyntheticChildren)
4060     {
4061             if (m_synthetic_value)
4062                 m_synthetic_value = NULL;
4063     }
4064 
4065     if ((clear_mask & eClearUserVisibleDataItemsValidator) == eClearUserVisibleDataItemsValidator)
4066         m_validation_result.reset();
4067 }
4068 
4069 SymbolContextScope *
4070 ValueObject::GetSymbolContextScope()
4071 {
4072     if (m_parent)
4073     {
4074         if (!m_parent->IsPointerOrReferenceType())
4075             return m_parent->GetSymbolContextScope();
4076     }
4077     return NULL;
4078 }
4079 
4080 lldb::ValueObjectSP
4081 ValueObject::CreateValueObjectFromExpression (const char* name,
4082                                               const char* expression,
4083                                               const ExecutionContext& exe_ctx)
4084 {
4085     return CreateValueObjectFromExpression(name, expression, exe_ctx, EvaluateExpressionOptions());
4086 }
4087 
4088 
4089 lldb::ValueObjectSP
4090 ValueObject::CreateValueObjectFromExpression (const char* name,
4091                                               const char* expression,
4092                                               const ExecutionContext& exe_ctx,
4093                                               const EvaluateExpressionOptions& options)
4094 {
4095     lldb::ValueObjectSP retval_sp;
4096     lldb::TargetSP target_sp(exe_ctx.GetTargetSP());
4097     if (!target_sp)
4098         return retval_sp;
4099     if (!expression || !*expression)
4100         return retval_sp;
4101     target_sp->EvaluateExpression (expression,
4102                                    exe_ctx.GetFrameSP().get(),
4103                                    retval_sp,
4104                                    options);
4105     if (retval_sp && name && *name)
4106         retval_sp->SetName(ConstString(name));
4107     return retval_sp;
4108 }
4109 
4110 lldb::ValueObjectSP
4111 ValueObject::CreateValueObjectFromAddress (const char* name,
4112                                            uint64_t address,
4113                                            const ExecutionContext& exe_ctx,
4114                                            CompilerType type)
4115 {
4116     if (type)
4117     {
4118         CompilerType pointer_type(type.GetPointerType());
4119         if (pointer_type)
4120         {
4121             lldb::DataBufferSP buffer(new lldb_private::DataBufferHeap(&address,sizeof(lldb::addr_t)));
4122             lldb::ValueObjectSP ptr_result_valobj_sp(ValueObjectConstResult::Create (exe_ctx.GetBestExecutionContextScope(),
4123                                                                                      pointer_type,
4124                                                                                      ConstString(name),
4125                                                                                      buffer,
4126                                                                                      exe_ctx.GetByteOrder(),
4127                                                                                      exe_ctx.GetAddressByteSize()));
4128             if (ptr_result_valobj_sp)
4129             {
4130                 ptr_result_valobj_sp->GetValue().SetValueType(Value::eValueTypeLoadAddress);
4131                 Error err;
4132                 ptr_result_valobj_sp = ptr_result_valobj_sp->Dereference(err);
4133                 if (ptr_result_valobj_sp && name && *name)
4134                     ptr_result_valobj_sp->SetName(ConstString(name));
4135             }
4136             return ptr_result_valobj_sp;
4137         }
4138     }
4139     return lldb::ValueObjectSP();
4140 }
4141 
4142 lldb::ValueObjectSP
4143 ValueObject::CreateValueObjectFromData (const char* name,
4144                                         const DataExtractor& data,
4145                                         const ExecutionContext& exe_ctx,
4146                                         CompilerType type)
4147 {
4148     lldb::ValueObjectSP new_value_sp;
4149     new_value_sp = ValueObjectConstResult::Create (exe_ctx.GetBestExecutionContextScope(),
4150                                                    type,
4151                                                    ConstString(name),
4152                                                    data,
4153                                                    LLDB_INVALID_ADDRESS);
4154     new_value_sp->SetAddressTypeOfChildren(eAddressTypeLoad);
4155     if (new_value_sp && name && *name)
4156         new_value_sp->SetName(ConstString(name));
4157     return new_value_sp;
4158 }
4159 
4160 ModuleSP
4161 ValueObject::GetModule ()
4162 {
4163     ValueObject* root(GetRoot());
4164     if (root != this)
4165         return root->GetModule();
4166     return lldb::ModuleSP();
4167 }
4168 
4169 ValueObject*
4170 ValueObject::GetRoot ()
4171 {
4172     if (m_root)
4173         return m_root;
4174     return (m_root = FollowParentChain( [] (ValueObject* vo) -> bool {
4175         return (vo->m_parent != nullptr);
4176     }));
4177 }
4178 
4179 ValueObject*
4180 ValueObject::FollowParentChain (std::function<bool(ValueObject*)> f)
4181 {
4182     ValueObject* vo = this;
4183     while (vo)
4184     {
4185         if (f(vo) == false)
4186             break;
4187         vo = vo->m_parent;
4188     }
4189     return vo;
4190 }
4191 
4192 AddressType
4193 ValueObject::GetAddressTypeOfChildren()
4194 {
4195     if (m_address_type_of_ptr_or_ref_children == eAddressTypeInvalid)
4196     {
4197         ValueObject* root(GetRoot());
4198         if (root != this)
4199             return root->GetAddressTypeOfChildren();
4200     }
4201     return m_address_type_of_ptr_or_ref_children;
4202 }
4203 
4204 lldb::DynamicValueType
4205 ValueObject::GetDynamicValueType ()
4206 {
4207     ValueObject* with_dv_info = this;
4208     while (with_dv_info)
4209     {
4210         if (with_dv_info->HasDynamicValueTypeInfo())
4211             return with_dv_info->GetDynamicValueTypeImpl();
4212         with_dv_info = with_dv_info->m_parent;
4213     }
4214     return lldb::eNoDynamicValues;
4215 }
4216 
4217 lldb::Format
4218 ValueObject::GetFormat () const
4219 {
4220     const ValueObject* with_fmt_info = this;
4221     while (with_fmt_info)
4222     {
4223         if (with_fmt_info->m_format != lldb::eFormatDefault)
4224             return with_fmt_info->m_format;
4225         with_fmt_info = with_fmt_info->m_parent;
4226     }
4227     return m_format;
4228 }
4229 
4230 lldb::LanguageType
4231 ValueObject::GetPreferredDisplayLanguage ()
4232 {
4233     lldb::LanguageType type = m_preferred_display_language;
4234     if (m_preferred_display_language == lldb::eLanguageTypeUnknown)
4235     {
4236         if (GetRoot())
4237         {
4238             if (GetRoot() == this)
4239             {
4240                 if (StackFrameSP frame_sp = GetFrameSP())
4241                 {
4242                     const SymbolContext& sc(frame_sp->GetSymbolContext(eSymbolContextCompUnit));
4243                     if (CompileUnit* cu = sc.comp_unit)
4244                         type = cu->GetLanguage();
4245                 }
4246             }
4247             else
4248             {
4249                 type = GetRoot()->GetPreferredDisplayLanguage();
4250             }
4251         }
4252     }
4253     return (m_preferred_display_language = type); // only compute it once
4254 }
4255 
4256 void
4257 ValueObject::SetPreferredDisplayLanguage (lldb::LanguageType lt)
4258 {
4259     m_preferred_display_language = lt;
4260 }
4261 
4262 void
4263 ValueObject::SetPreferredDisplayLanguageIfNeeded (lldb::LanguageType lt)
4264 {
4265     if (m_preferred_display_language == lldb::eLanguageTypeUnknown)
4266         SetPreferredDisplayLanguage(lt);
4267 }
4268 
4269 bool
4270 ValueObject::CanProvideValue ()
4271 {
4272     // we need to support invalid types as providers of values because some bare-board
4273     // debugging scenarios have no notion of types, but still manage to have raw numeric
4274     // values for things like registers. sigh.
4275     const CompilerType &type(GetCompilerType());
4276     return (false == type.IsValid()) || (0 != (type.GetTypeInfo() & eTypeHasValue));
4277 }
4278 
4279 bool
4280 ValueObject::IsChecksumEmpty ()
4281 {
4282     return m_value_checksum.empty();
4283 }
4284 
4285 ValueObjectSP
4286 ValueObject::Persist ()
4287 {
4288     if (!UpdateValueIfNeeded())
4289         return nullptr;
4290 
4291     TargetSP target_sp(GetTargetSP());
4292     if (!target_sp)
4293         return nullptr;
4294 
4295     PersistentExpressionState *persistent_state = target_sp->GetPersistentExpressionStateForLanguage(GetPreferredDisplayLanguage());
4296 
4297     if (!persistent_state)
4298         return nullptr;
4299 
4300     ConstString name(persistent_state->GetNextPersistentVariableName());
4301 
4302     ValueObjectSP const_result_sp = ValueObjectConstResult::Create (target_sp.get(), GetValue(), name);
4303 
4304     ExpressionVariableSP clang_var_sp = persistent_state->CreatePersistentVariable(const_result_sp);
4305     clang_var_sp->m_live_sp = clang_var_sp->m_frozen_sp;
4306     clang_var_sp->m_flags |= ExpressionVariable::EVIsProgramReference;
4307 
4308     return clang_var_sp->GetValueObject();
4309 }
4310 
4311 bool
4312 ValueObject::IsSyntheticChildrenGenerated ()
4313 {
4314     return m_is_synthetic_children_generated;
4315 }
4316 
4317 void
4318 ValueObject::SetSyntheticChildrenGenerated (bool b)
4319 {
4320     m_is_synthetic_children_generated = b;
4321 }
4322