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