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