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