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