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