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, error);
982         }
983         else
984         {
985             ValueObjectSP child_sp = GetChildAtIndex(0, true);
986             if (child_sp.get() == NULL)
987                 return 0;
988             Error error;
989             return child_sp->GetData(data, error);
990         }
991         return true;
992     }
993     else /* (items > 1) */
994     {
995         Error error;
996         lldb_private::DataBufferHeap* heap_buf_ptr = NULL;
997         lldb::DataBufferSP data_sp(heap_buf_ptr = new lldb_private::DataBufferHeap());
998 
999         AddressType addr_type;
1000         lldb::addr_t addr = is_pointer_type ? GetPointerValue(&addr_type) : GetAddressOf(true, &addr_type);
1001 
1002         switch (addr_type)
1003         {
1004             case eAddressTypeFile:
1005                 {
1006                     ModuleSP module_sp (GetModule());
1007                     if (module_sp)
1008                     {
1009                         addr = addr + offset;
1010                         Address so_addr;
1011                         module_sp->ResolveFileAddress(addr, so_addr);
1012                         ExecutionContext exe_ctx (GetExecutionContextRef());
1013                         Target* target = exe_ctx.GetTargetPtr();
1014                         if (target)
1015                         {
1016                             heap_buf_ptr->SetByteSize(bytes);
1017                             size_t bytes_read = target->ReadMemory(so_addr, false, heap_buf_ptr->GetBytes(), bytes, error);
1018                             if (error.Success())
1019                             {
1020                                 data.SetData(data_sp);
1021                                 return bytes_read;
1022                             }
1023                         }
1024                     }
1025                 }
1026                 break;
1027             case eAddressTypeLoad:
1028                 {
1029                     ExecutionContext exe_ctx (GetExecutionContextRef());
1030                     Process *process = exe_ctx.GetProcessPtr();
1031                     if (process)
1032                     {
1033                         heap_buf_ptr->SetByteSize(bytes);
1034                         size_t bytes_read = process->ReadMemory(addr + offset, heap_buf_ptr->GetBytes(), bytes, error);
1035                         if (error.Success() || bytes_read > 0)
1036                         {
1037                             data.SetData(data_sp);
1038                             return bytes_read;
1039                         }
1040                     }
1041                 }
1042                 break;
1043             case eAddressTypeHost:
1044                 {
1045                     const uint64_t max_bytes = GetClangType().GetByteSize();
1046                     if (max_bytes > offset)
1047                     {
1048                         size_t bytes_read = std::min<uint64_t>(max_bytes - offset, bytes);
1049                         heap_buf_ptr->CopyData((uint8_t*)(addr + offset), bytes_read);
1050                         data.SetData(data_sp);
1051                         return bytes_read;
1052                     }
1053                 }
1054                 break;
1055             case eAddressTypeInvalid:
1056                 break;
1057         }
1058     }
1059     return 0;
1060 }
1061 
1062 uint64_t
1063 ValueObject::GetData (DataExtractor& data, Error &error)
1064 {
1065     UpdateValueIfNeeded(false);
1066     ExecutionContext exe_ctx (GetExecutionContextRef());
1067     error = m_value.GetValueAsData(&exe_ctx, data, 0, GetModule().get());
1068     if (error.Fail())
1069     {
1070         if (m_data.GetByteSize())
1071         {
1072             data = m_data;
1073             return data.GetByteSize();
1074         }
1075         else
1076         {
1077             return 0;
1078         }
1079     }
1080     data.SetAddressByteSize(m_data.GetAddressByteSize());
1081     data.SetByteOrder(m_data.GetByteOrder());
1082     return data.GetByteSize();
1083 }
1084 
1085 bool
1086 ValueObject::SetData (DataExtractor &data, Error &error)
1087 {
1088     error.Clear();
1089     // Make sure our value is up to date first so that our location and location
1090     // type is valid.
1091     if (!UpdateValueIfNeeded(false))
1092     {
1093         error.SetErrorString("unable to read value");
1094         return false;
1095     }
1096 
1097     uint64_t count = 0;
1098     const Encoding encoding = GetClangType().GetEncoding(count);
1099 
1100     const size_t byte_size = GetByteSize();
1101 
1102     Value::ValueType value_type = m_value.GetValueType();
1103 
1104     switch (value_type)
1105     {
1106     case Value::eValueTypeScalar:
1107         {
1108             Error set_error = m_value.GetScalar().SetValueFromData(data, encoding, byte_size);
1109 
1110             if (!set_error.Success())
1111             {
1112                 error.SetErrorStringWithFormat("unable to set scalar value: %s", set_error.AsCString());
1113                 return false;
1114             }
1115         }
1116         break;
1117     case Value::eValueTypeLoadAddress:
1118         {
1119             // If it is a load address, then the scalar value is the storage location
1120             // of the data, and we have to shove this value down to that load location.
1121             ExecutionContext exe_ctx (GetExecutionContextRef());
1122             Process *process = exe_ctx.GetProcessPtr();
1123             if (process)
1124             {
1125                 addr_t target_addr = m_value.GetScalar().ULongLong(LLDB_INVALID_ADDRESS);
1126                 size_t bytes_written = process->WriteMemory(target_addr,
1127                                                             data.GetDataStart(),
1128                                                             byte_size,
1129                                                             error);
1130                 if (!error.Success())
1131                     return false;
1132                 if (bytes_written != byte_size)
1133                 {
1134                     error.SetErrorString("unable to write value to memory");
1135                     return false;
1136                 }
1137             }
1138         }
1139         break;
1140     case Value::eValueTypeHostAddress:
1141         {
1142             // If it is a host address, then we stuff the scalar as a DataBuffer into the Value's data.
1143             DataBufferSP buffer_sp (new DataBufferHeap(byte_size, 0));
1144             m_data.SetData(buffer_sp, 0);
1145             data.CopyByteOrderedData (0,
1146                                       byte_size,
1147                                       const_cast<uint8_t *>(m_data.GetDataStart()),
1148                                       byte_size,
1149                                       m_data.GetByteOrder());
1150             m_value.GetScalar() = (uintptr_t)m_data.GetDataStart();
1151         }
1152         break;
1153     case Value::eValueTypeFileAddress:
1154     case Value::eValueTypeVector:
1155         break;
1156     }
1157 
1158     // If we have reached this point, then we have successfully changed the value.
1159     SetNeedsUpdate();
1160     return true;
1161 }
1162 
1163 // will compute strlen(str), but without consuming more than
1164 // maxlen bytes out of str (this serves the purpose of reading
1165 // chunks of a string without having to worry about
1166 // missing NULL terminators in the chunk)
1167 // of course, if strlen(str) > maxlen, the function will return
1168 // maxlen_value (which should be != maxlen, because that allows you
1169 // to know whether strlen(str) == maxlen or strlen(str) > maxlen)
1170 static uint32_t
1171 strlen_or_inf (const char* str,
1172                uint32_t maxlen,
1173                uint32_t maxlen_value)
1174 {
1175     uint32_t len = 0;
1176     if (str)
1177     {
1178         while(*str)
1179         {
1180             len++;str++;
1181             if (len >= maxlen)
1182                 return maxlen_value;
1183         }
1184     }
1185     return len;
1186 }
1187 
1188 size_t
1189 ValueObject::ReadPointedString (Stream& s,
1190                                 Error& error,
1191                                 uint32_t max_length,
1192                                 bool honor_array,
1193                                 Format item_format)
1194 {
1195     ExecutionContext exe_ctx (GetExecutionContextRef());
1196     Target* target = exe_ctx.GetTargetPtr();
1197 
1198     if (!target)
1199     {
1200         s << "<no target to read from>";
1201         error.SetErrorString("no target to read from");
1202         return 0;
1203     }
1204 
1205     if (max_length == 0)
1206         max_length = target->GetMaximumSizeOfStringSummary();
1207 
1208     size_t bytes_read = 0;
1209     size_t total_bytes_read = 0;
1210 
1211     ClangASTType clang_type = GetClangType();
1212     ClangASTType elem_or_pointee_clang_type;
1213     const Flags type_flags (GetTypeInfo (&elem_or_pointee_clang_type));
1214     if (type_flags.AnySet (ClangASTType::eTypeIsArray | ClangASTType::eTypeIsPointer) &&
1215         elem_or_pointee_clang_type.IsCharType ())
1216     {
1217         addr_t cstr_address = LLDB_INVALID_ADDRESS;
1218         AddressType cstr_address_type = eAddressTypeInvalid;
1219 
1220         size_t cstr_len = 0;
1221         bool capped_data = false;
1222         if (type_flags.Test (ClangASTType::eTypeIsArray))
1223         {
1224             // We have an array
1225             uint64_t array_size = 0;
1226             if (clang_type.IsArrayType(NULL, &array_size, NULL))
1227             {
1228                 cstr_len = array_size;
1229                 if (cstr_len > max_length)
1230                 {
1231                     capped_data = true;
1232                     cstr_len = max_length;
1233                 }
1234             }
1235             cstr_address = GetAddressOf (true, &cstr_address_type);
1236         }
1237         else
1238         {
1239             // We have a pointer
1240             cstr_address = GetPointerValue (&cstr_address_type);
1241         }
1242 
1243         if (cstr_address == 0 || cstr_address == LLDB_INVALID_ADDRESS)
1244         {
1245             s << "<invalid address>";
1246             error.SetErrorString("invalid address");
1247             return 0;
1248         }
1249 
1250         Address cstr_so_addr (cstr_address);
1251         DataExtractor data;
1252         if (cstr_len > 0 && honor_array)
1253         {
1254             // I am using GetPointeeData() here to abstract the fact that some ValueObjects are actually frozen pointers in the host
1255             // but the pointed-to data lives in the debuggee, and GetPointeeData() automatically takes care of this
1256             GetPointeeData(data, 0, cstr_len);
1257 
1258             if ((bytes_read = data.GetByteSize()) > 0)
1259             {
1260                 total_bytes_read = bytes_read;
1261                 s << '"';
1262                 data.Dump (&s,
1263                            0,                 // Start offset in "data"
1264                            item_format,
1265                            1,                 // Size of item (1 byte for a char!)
1266                            bytes_read,        // How many bytes to print?
1267                            UINT32_MAX,        // num per line
1268                            LLDB_INVALID_ADDRESS,// base address
1269                            0,                 // bitfield bit size
1270                            0);                // bitfield bit offset
1271                 if (capped_data)
1272                     s << "...";
1273                 s << '"';
1274             }
1275         }
1276         else
1277         {
1278             cstr_len = max_length;
1279             const size_t k_max_buf_size = 64;
1280 
1281             size_t offset = 0;
1282 
1283             int cstr_len_displayed = -1;
1284             bool capped_cstr = false;
1285             // I am using GetPointeeData() here to abstract the fact that some ValueObjects are actually frozen pointers in the host
1286             // but the pointed-to data lives in the debuggee, and GetPointeeData() automatically takes care of this
1287             while ((bytes_read = GetPointeeData(data, offset, k_max_buf_size)) > 0)
1288             {
1289                 total_bytes_read += bytes_read;
1290                 const char *cstr = data.PeekCStr(0);
1291                 size_t len = strlen_or_inf (cstr, k_max_buf_size, k_max_buf_size+1);
1292                 if (len > k_max_buf_size)
1293                     len = k_max_buf_size;
1294                 if (cstr && cstr_len_displayed < 0)
1295                     s << '"';
1296 
1297                 if (cstr_len_displayed < 0)
1298                     cstr_len_displayed = len;
1299 
1300                 if (len == 0)
1301                     break;
1302                 cstr_len_displayed += len;
1303                 if (len > bytes_read)
1304                     len = bytes_read;
1305                 if (len > cstr_len)
1306                     len = cstr_len;
1307 
1308                 data.Dump (&s,
1309                            0,                 // Start offset in "data"
1310                            item_format,
1311                            1,                 // Size of item (1 byte for a char!)
1312                            len,               // How many bytes to print?
1313                            UINT32_MAX,        // num per line
1314                            LLDB_INVALID_ADDRESS,// base address
1315                            0,                 // bitfield bit size
1316                            0);                // bitfield bit offset
1317 
1318                 if (len < k_max_buf_size)
1319                     break;
1320 
1321                 if (len >= cstr_len)
1322                 {
1323                     capped_cstr = true;
1324                     break;
1325                 }
1326 
1327                 cstr_len -= len;
1328                 offset += len;
1329             }
1330 
1331             if (cstr_len_displayed >= 0)
1332             {
1333                 s << '"';
1334                 if (capped_cstr)
1335                     s << "...";
1336             }
1337         }
1338     }
1339     else
1340     {
1341         error.SetErrorString("not a string object");
1342         s << "<not a string object>";
1343     }
1344     return total_bytes_read;
1345 }
1346 
1347 const char *
1348 ValueObject::GetObjectDescription ()
1349 {
1350 
1351     if (!UpdateValueIfNeeded (true))
1352         return NULL;
1353 
1354     if (!m_object_desc_str.empty())
1355         return m_object_desc_str.c_str();
1356 
1357     ExecutionContext exe_ctx (GetExecutionContextRef());
1358     Process *process = exe_ctx.GetProcessPtr();
1359     if (process == NULL)
1360         return NULL;
1361 
1362     StreamString s;
1363 
1364     LanguageType language = GetObjectRuntimeLanguage();
1365     LanguageRuntime *runtime = process->GetLanguageRuntime(language);
1366 
1367     if (runtime == NULL)
1368     {
1369         // Aw, hell, if the things a pointer, or even just an integer, let's try ObjC anyway...
1370         ClangASTType clang_type = GetClangType();
1371         if (clang_type)
1372         {
1373             bool is_signed;
1374             if (clang_type.IsIntegerType (is_signed) || clang_type.IsPointerType ())
1375             {
1376                 runtime = process->GetLanguageRuntime(eLanguageTypeObjC);
1377             }
1378         }
1379     }
1380 
1381     if (runtime && runtime->GetObjectDescription(s, *this))
1382     {
1383         m_object_desc_str.append (s.GetData());
1384     }
1385 
1386     if (m_object_desc_str.empty())
1387         return NULL;
1388     else
1389         return m_object_desc_str.c_str();
1390 }
1391 
1392 bool
1393 ValueObject::GetValueAsCString (const lldb_private::TypeFormatImpl& format,
1394                                 std::string& destination)
1395 {
1396     if (UpdateValueIfNeeded(false))
1397         return format.FormatObject(this,destination);
1398     else
1399         return false;
1400 }
1401 
1402 bool
1403 ValueObject::GetValueAsCString (lldb::Format format,
1404                                 std::string& destination)
1405 {
1406     return GetValueAsCString(TypeFormatImpl_Format(format),destination);
1407 }
1408 
1409 const char *
1410 ValueObject::GetValueAsCString ()
1411 {
1412     if (UpdateValueIfNeeded(true))
1413     {
1414         lldb::TypeFormatImplSP format_sp;
1415         lldb::Format my_format = GetFormat();
1416         if (my_format == lldb::eFormatDefault)
1417         {
1418             if (m_type_format_sp)
1419                 format_sp = m_type_format_sp;
1420             else
1421             {
1422                 if (m_is_bitfield_for_scalar)
1423                     my_format = eFormatUnsigned;
1424                 else
1425                 {
1426                     if (m_value.GetContextType() == Value::eContextTypeRegisterInfo)
1427                     {
1428                         const RegisterInfo *reg_info = m_value.GetRegisterInfo();
1429                         if (reg_info)
1430                             my_format = reg_info->format;
1431                     }
1432                     else
1433                     {
1434                         my_format = GetClangType().GetFormat();
1435                     }
1436                 }
1437             }
1438         }
1439         if (my_format != m_last_format || m_value_str.empty())
1440         {
1441             m_last_format = my_format;
1442             if (!format_sp)
1443                 format_sp.reset(new TypeFormatImpl_Format(my_format));
1444             if (GetValueAsCString(*format_sp.get(), m_value_str))
1445             {
1446                 if (!m_value_did_change && m_old_value_valid)
1447                 {
1448                     // The value was gotten successfully, so we consider the
1449                     // value as changed if the value string differs
1450                     SetValueDidChange (m_old_value_str != m_value_str);
1451                 }
1452             }
1453         }
1454     }
1455     if (m_value_str.empty())
1456         return NULL;
1457     return m_value_str.c_str();
1458 }
1459 
1460 // if > 8bytes, 0 is returned. this method should mostly be used
1461 // to read address values out of pointers
1462 uint64_t
1463 ValueObject::GetValueAsUnsigned (uint64_t fail_value, bool *success)
1464 {
1465     // If our byte size is zero this is an aggregate type that has children
1466     if (!GetClangType().IsAggregateType())
1467     {
1468         Scalar scalar;
1469         if (ResolveValue (scalar))
1470         {
1471             if (success)
1472                 *success = true;
1473             return scalar.ULongLong(fail_value);
1474         }
1475         // fallthrough, otherwise...
1476     }
1477 
1478     if (success)
1479         *success = false;
1480     return fail_value;
1481 }
1482 
1483 int64_t
1484 ValueObject::GetValueAsSigned (int64_t fail_value, bool *success)
1485 {
1486     // If our byte size is zero this is an aggregate type that has children
1487     if (!GetClangType().IsAggregateType())
1488     {
1489         Scalar scalar;
1490         if (ResolveValue (scalar))
1491         {
1492             if (success)
1493                 *success = true;
1494                 return scalar.SLongLong(fail_value);
1495         }
1496         // fallthrough, otherwise...
1497     }
1498 
1499     if (success)
1500         *success = false;
1501         return fail_value;
1502 }
1503 
1504 // if any more "special cases" are added to ValueObject::DumpPrintableRepresentation() please keep
1505 // this call up to date by returning true for your new special cases. We will eventually move
1506 // to checking this call result before trying to display special cases
1507 bool
1508 ValueObject::HasSpecialPrintableRepresentation(ValueObjectRepresentationStyle val_obj_display,
1509                                                Format custom_format)
1510 {
1511     Flags flags(GetTypeInfo());
1512     if (flags.AnySet(ClangASTType::eTypeIsArray | ClangASTType::eTypeIsPointer)
1513         && val_obj_display == ValueObject::eValueObjectRepresentationStyleValue)
1514     {
1515         if (IsCStringContainer(true) &&
1516             (custom_format == eFormatCString ||
1517              custom_format == eFormatCharArray ||
1518              custom_format == eFormatChar ||
1519              custom_format == eFormatVectorOfChar))
1520             return true;
1521 
1522         if (flags.Test(ClangASTType::eTypeIsArray))
1523         {
1524             if ((custom_format == eFormatBytes) ||
1525                 (custom_format == eFormatBytesWithASCII))
1526                 return true;
1527 
1528             if ((custom_format == eFormatVectorOfChar) ||
1529                 (custom_format == eFormatVectorOfFloat32) ||
1530                 (custom_format == eFormatVectorOfFloat64) ||
1531                 (custom_format == eFormatVectorOfSInt16) ||
1532                 (custom_format == eFormatVectorOfSInt32) ||
1533                 (custom_format == eFormatVectorOfSInt64) ||
1534                 (custom_format == eFormatVectorOfSInt8) ||
1535                 (custom_format == eFormatVectorOfUInt128) ||
1536                 (custom_format == eFormatVectorOfUInt16) ||
1537                 (custom_format == eFormatVectorOfUInt32) ||
1538                 (custom_format == eFormatVectorOfUInt64) ||
1539                 (custom_format == eFormatVectorOfUInt8))
1540                 return true;
1541         }
1542     }
1543     return false;
1544 }
1545 
1546 bool
1547 ValueObject::DumpPrintableRepresentation(Stream& s,
1548                                          ValueObjectRepresentationStyle val_obj_display,
1549                                          Format custom_format,
1550                                          PrintableRepresentationSpecialCases special,
1551                                          bool do_dump_error)
1552 {
1553 
1554     Flags flags(GetTypeInfo());
1555 
1556     bool allow_special = ((special & ePrintableRepresentationSpecialCasesAllow) == ePrintableRepresentationSpecialCasesAllow);
1557     bool only_special = ((special & ePrintableRepresentationSpecialCasesOnly) == ePrintableRepresentationSpecialCasesOnly);
1558 
1559     if (allow_special)
1560     {
1561         if (flags.AnySet(ClangASTType::eTypeIsArray | ClangASTType::eTypeIsPointer)
1562              && val_obj_display == ValueObject::eValueObjectRepresentationStyleValue)
1563         {
1564             // when being asked to get a printable display an array or pointer type directly,
1565             // try to "do the right thing"
1566 
1567             if (IsCStringContainer(true) &&
1568                 (custom_format == eFormatCString ||
1569                  custom_format == eFormatCharArray ||
1570                  custom_format == eFormatChar ||
1571                  custom_format == eFormatVectorOfChar)) // print char[] & char* directly
1572             {
1573                 Error error;
1574                 ReadPointedString(s,
1575                                   error,
1576                                   0,
1577                                   (custom_format == eFormatVectorOfChar) ||
1578                                   (custom_format == eFormatCharArray));
1579                 return !error.Fail();
1580             }
1581 
1582             if (custom_format == eFormatEnum)
1583                 return false;
1584 
1585             // this only works for arrays, because I have no way to know when
1586             // the pointed memory ends, and no special \0 end of data marker
1587             if (flags.Test(ClangASTType::eTypeIsArray))
1588             {
1589                 if ((custom_format == eFormatBytes) ||
1590                     (custom_format == eFormatBytesWithASCII))
1591                 {
1592                     const size_t count = GetNumChildren();
1593 
1594                     s << '[';
1595                     for (size_t low = 0; low < count; low++)
1596                     {
1597 
1598                         if (low)
1599                             s << ',';
1600 
1601                         ValueObjectSP child = GetChildAtIndex(low,true);
1602                         if (!child.get())
1603                         {
1604                             s << "<invalid child>";
1605                             continue;
1606                         }
1607                         child->DumpPrintableRepresentation(s, ValueObject::eValueObjectRepresentationStyleValue, custom_format);
1608                     }
1609 
1610                     s << ']';
1611 
1612                     return true;
1613                 }
1614 
1615                 if ((custom_format == eFormatVectorOfChar) ||
1616                     (custom_format == eFormatVectorOfFloat32) ||
1617                     (custom_format == eFormatVectorOfFloat64) ||
1618                     (custom_format == eFormatVectorOfSInt16) ||
1619                     (custom_format == eFormatVectorOfSInt32) ||
1620                     (custom_format == eFormatVectorOfSInt64) ||
1621                     (custom_format == eFormatVectorOfSInt8) ||
1622                     (custom_format == eFormatVectorOfUInt128) ||
1623                     (custom_format == eFormatVectorOfUInt16) ||
1624                     (custom_format == eFormatVectorOfUInt32) ||
1625                     (custom_format == eFormatVectorOfUInt64) ||
1626                     (custom_format == eFormatVectorOfUInt8)) // arrays of bytes, bytes with ASCII or any vector format should be printed directly
1627                 {
1628                     const size_t count = GetNumChildren();
1629 
1630                     Format format = FormatManager::GetSingleItemFormat(custom_format);
1631 
1632                     s << '[';
1633                     for (size_t low = 0; low < count; low++)
1634                     {
1635 
1636                         if (low)
1637                             s << ',';
1638 
1639                         ValueObjectSP child = GetChildAtIndex(low,true);
1640                         if (!child.get())
1641                         {
1642                             s << "<invalid child>";
1643                             continue;
1644                         }
1645                         child->DumpPrintableRepresentation(s, ValueObject::eValueObjectRepresentationStyleValue, format);
1646                     }
1647 
1648                     s << ']';
1649 
1650                     return true;
1651                 }
1652             }
1653 
1654             if ((custom_format == eFormatBoolean) ||
1655                 (custom_format == eFormatBinary) ||
1656                 (custom_format == eFormatChar) ||
1657                 (custom_format == eFormatCharPrintable) ||
1658                 (custom_format == eFormatComplexFloat) ||
1659                 (custom_format == eFormatDecimal) ||
1660                 (custom_format == eFormatHex) ||
1661                 (custom_format == eFormatHexUppercase) ||
1662                 (custom_format == eFormatFloat) ||
1663                 (custom_format == eFormatOctal) ||
1664                 (custom_format == eFormatOSType) ||
1665                 (custom_format == eFormatUnicode16) ||
1666                 (custom_format == eFormatUnicode32) ||
1667                 (custom_format == eFormatUnsigned) ||
1668                 (custom_format == eFormatPointer) ||
1669                 (custom_format == eFormatComplexInteger) ||
1670                 (custom_format == eFormatComplex) ||
1671                 (custom_format == eFormatDefault)) // use the [] operator
1672                 return false;
1673         }
1674     }
1675 
1676     if (only_special)
1677         return false;
1678 
1679     bool var_success = false;
1680 
1681     {
1682         const char *cstr = NULL;
1683 
1684          // this is a local stream that we are using to ensure that the data pointed to by cstr survives
1685         // long enough for us to copy it to its destination - it is necessary to have this temporary storage
1686         // area for cases where our desired output is not backed by some other longer-term storage
1687         StreamString strm;
1688 
1689         if (custom_format != eFormatInvalid)
1690             SetFormat(custom_format);
1691 
1692         switch(val_obj_display)
1693         {
1694             case eValueObjectRepresentationStyleValue:
1695                 cstr = GetValueAsCString();
1696                 break;
1697 
1698             case eValueObjectRepresentationStyleSummary:
1699                 cstr = GetSummaryAsCString();
1700                 break;
1701 
1702             case eValueObjectRepresentationStyleLanguageSpecific:
1703                 cstr = GetObjectDescription();
1704                 break;
1705 
1706             case eValueObjectRepresentationStyleLocation:
1707                 cstr = GetLocationAsCString();
1708                 break;
1709 
1710             case eValueObjectRepresentationStyleChildrenCount:
1711                 strm.Printf("%" PRIu64 "", (uint64_t)GetNumChildren());
1712                 cstr = strm.GetString().c_str();
1713                 break;
1714 
1715             case eValueObjectRepresentationStyleType:
1716                 cstr = GetTypeName().AsCString();
1717                 break;
1718 
1719             case eValueObjectRepresentationStyleName:
1720                 cstr = GetName().AsCString();
1721                 break;
1722 
1723             case eValueObjectRepresentationStyleExpressionPath:
1724                 GetExpressionPath(strm, false);
1725                 cstr = strm.GetString().c_str();
1726                 break;
1727         }
1728 
1729         if (!cstr)
1730         {
1731             if (val_obj_display == eValueObjectRepresentationStyleValue)
1732                 cstr = GetSummaryAsCString();
1733             else if (val_obj_display == eValueObjectRepresentationStyleSummary)
1734             {
1735                 if (GetClangType().IsAggregateType())
1736                 {
1737                     strm.Printf("%s @ %s", GetTypeName().AsCString(), GetLocationAsCString());
1738                     cstr = strm.GetString().c_str();
1739                 }
1740                 else
1741                     cstr = GetValueAsCString();
1742             }
1743         }
1744 
1745         if (cstr)
1746             s.PutCString(cstr);
1747         else
1748         {
1749             if (m_error.Fail())
1750             {
1751                 if (do_dump_error)
1752                     s.Printf("<%s>", m_error.AsCString());
1753                 else
1754                     return false;
1755             }
1756             else if (val_obj_display == eValueObjectRepresentationStyleSummary)
1757                 s.PutCString("<no summary available>");
1758             else if (val_obj_display == eValueObjectRepresentationStyleValue)
1759                 s.PutCString("<no value available>");
1760             else if (val_obj_display == eValueObjectRepresentationStyleLanguageSpecific)
1761                 s.PutCString("<not a valid Objective-C object>"); // edit this if we have other runtimes that support a description
1762             else
1763                 s.PutCString("<no printable representation>");
1764         }
1765 
1766         // we should only return false here if we could not do *anything*
1767         // even if we have an error message as output, that's a success
1768         // from our callers' perspective, so return true
1769         var_success = true;
1770 
1771         if (custom_format != eFormatInvalid)
1772             SetFormat(eFormatDefault);
1773     }
1774 
1775     return var_success;
1776 }
1777 
1778 addr_t
1779 ValueObject::GetAddressOf (bool scalar_is_load_address, AddressType *address_type)
1780 {
1781     if (!UpdateValueIfNeeded(false))
1782         return LLDB_INVALID_ADDRESS;
1783 
1784     switch (m_value.GetValueType())
1785     {
1786     case Value::eValueTypeScalar:
1787     case Value::eValueTypeVector:
1788         if (scalar_is_load_address)
1789         {
1790             if(address_type)
1791                 *address_type = eAddressTypeLoad;
1792             return m_value.GetScalar().ULongLong(LLDB_INVALID_ADDRESS);
1793         }
1794         break;
1795 
1796     case Value::eValueTypeLoadAddress:
1797     case Value::eValueTypeFileAddress:
1798     case Value::eValueTypeHostAddress:
1799         {
1800             if(address_type)
1801                 *address_type = m_value.GetValueAddressType ();
1802             return m_value.GetScalar().ULongLong(LLDB_INVALID_ADDRESS);
1803         }
1804         break;
1805     }
1806     if (address_type)
1807         *address_type = eAddressTypeInvalid;
1808     return LLDB_INVALID_ADDRESS;
1809 }
1810 
1811 addr_t
1812 ValueObject::GetPointerValue (AddressType *address_type)
1813 {
1814     addr_t address = LLDB_INVALID_ADDRESS;
1815     if(address_type)
1816         *address_type = eAddressTypeInvalid;
1817 
1818     if (!UpdateValueIfNeeded(false))
1819         return address;
1820 
1821     switch (m_value.GetValueType())
1822     {
1823     case Value::eValueTypeScalar:
1824     case Value::eValueTypeVector:
1825         address = m_value.GetScalar().ULongLong(LLDB_INVALID_ADDRESS);
1826         break;
1827 
1828     case Value::eValueTypeHostAddress:
1829     case Value::eValueTypeLoadAddress:
1830     case Value::eValueTypeFileAddress:
1831         {
1832             lldb::offset_t data_offset = 0;
1833             address = m_data.GetPointer(&data_offset);
1834         }
1835         break;
1836     }
1837 
1838     if (address_type)
1839         *address_type = GetAddressTypeOfChildren();
1840 
1841     return address;
1842 }
1843 
1844 bool
1845 ValueObject::SetValueFromCString (const char *value_str, Error& error)
1846 {
1847     error.Clear();
1848     // Make sure our value is up to date first so that our location and location
1849     // type is valid.
1850     if (!UpdateValueIfNeeded(false))
1851     {
1852         error.SetErrorString("unable to read value");
1853         return false;
1854     }
1855 
1856     uint64_t count = 0;
1857     const Encoding encoding = GetClangType().GetEncoding (count);
1858 
1859     const size_t byte_size = GetByteSize();
1860 
1861     Value::ValueType value_type = m_value.GetValueType();
1862 
1863     if (value_type == Value::eValueTypeScalar)
1864     {
1865         // If the value is already a scalar, then let the scalar change itself:
1866         m_value.GetScalar().SetValueFromCString (value_str, encoding, byte_size);
1867     }
1868     else if (byte_size <= Scalar::GetMaxByteSize())
1869     {
1870         // If the value fits in a scalar, then make a new scalar and again let the
1871         // scalar code do the conversion, then figure out where to put the new value.
1872         Scalar new_scalar;
1873         error = new_scalar.SetValueFromCString (value_str, encoding, byte_size);
1874         if (error.Success())
1875         {
1876             switch (value_type)
1877             {
1878             case Value::eValueTypeLoadAddress:
1879                 {
1880                     // If it is a load address, then the scalar value is the storage location
1881                     // of the data, and we have to shove this value down to that load location.
1882                     ExecutionContext exe_ctx (GetExecutionContextRef());
1883                     Process *process = exe_ctx.GetProcessPtr();
1884                     if (process)
1885                     {
1886                         addr_t target_addr = m_value.GetScalar().ULongLong(LLDB_INVALID_ADDRESS);
1887                         size_t bytes_written = process->WriteScalarToMemory (target_addr,
1888                                                                              new_scalar,
1889                                                                              byte_size,
1890                                                                              error);
1891                         if (!error.Success())
1892                             return false;
1893                         if (bytes_written != byte_size)
1894                         {
1895                             error.SetErrorString("unable to write value to memory");
1896                             return false;
1897                         }
1898                     }
1899                 }
1900                 break;
1901             case Value::eValueTypeHostAddress:
1902                 {
1903                     // If it is a host address, then we stuff the scalar as a DataBuffer into the Value's data.
1904                     DataExtractor new_data;
1905                     new_data.SetByteOrder (m_data.GetByteOrder());
1906 
1907                     DataBufferSP buffer_sp (new DataBufferHeap(byte_size, 0));
1908                     m_data.SetData(buffer_sp, 0);
1909                     bool success = new_scalar.GetData(new_data);
1910                     if (success)
1911                     {
1912                         new_data.CopyByteOrderedData (0,
1913                                                       byte_size,
1914                                                       const_cast<uint8_t *>(m_data.GetDataStart()),
1915                                                       byte_size,
1916                                                       m_data.GetByteOrder());
1917                     }
1918                     m_value.GetScalar() = (uintptr_t)m_data.GetDataStart();
1919 
1920                 }
1921                 break;
1922             case Value::eValueTypeFileAddress:
1923             case Value::eValueTypeScalar:
1924             case Value::eValueTypeVector:
1925                 break;
1926             }
1927         }
1928         else
1929         {
1930             return false;
1931         }
1932     }
1933     else
1934     {
1935         // We don't support setting things bigger than a scalar at present.
1936         error.SetErrorString("unable to write aggregate data type");
1937         return false;
1938     }
1939 
1940     // If we have reached this point, then we have successfully changed the value.
1941     SetNeedsUpdate();
1942     return true;
1943 }
1944 
1945 bool
1946 ValueObject::GetDeclaration (Declaration &decl)
1947 {
1948     decl.Clear();
1949     return false;
1950 }
1951 
1952 ConstString
1953 ValueObject::GetTypeName()
1954 {
1955     return GetClangType().GetConstTypeName();
1956 }
1957 
1958 ConstString
1959 ValueObject::GetQualifiedTypeName()
1960 {
1961     return GetClangType().GetConstQualifiedTypeName();
1962 }
1963 
1964 
1965 LanguageType
1966 ValueObject::GetObjectRuntimeLanguage ()
1967 {
1968     return GetClangType().GetMinimumLanguage ();
1969 }
1970 
1971 void
1972 ValueObject::AddSyntheticChild (const ConstString &key, ValueObject *valobj)
1973 {
1974     m_synthetic_children[key] = valobj;
1975 }
1976 
1977 ValueObjectSP
1978 ValueObject::GetSyntheticChild (const ConstString &key) const
1979 {
1980     ValueObjectSP synthetic_child_sp;
1981     std::map<ConstString, ValueObject *>::const_iterator pos = m_synthetic_children.find (key);
1982     if (pos != m_synthetic_children.end())
1983         synthetic_child_sp = pos->second->GetSP();
1984     return synthetic_child_sp;
1985 }
1986 
1987 uint32_t
1988 ValueObject::GetTypeInfo (ClangASTType *pointee_or_element_clang_type)
1989 {
1990     return GetClangType().GetTypeInfo (pointee_or_element_clang_type);
1991 }
1992 
1993 bool
1994 ValueObject::IsPointerType ()
1995 {
1996     return GetClangType().IsPointerType();
1997 }
1998 
1999 bool
2000 ValueObject::IsArrayType ()
2001 {
2002     return GetClangType().IsArrayType (NULL, NULL, NULL);
2003 }
2004 
2005 bool
2006 ValueObject::IsScalarType ()
2007 {
2008     return GetClangType().IsScalarType ();
2009 }
2010 
2011 bool
2012 ValueObject::IsIntegerType (bool &is_signed)
2013 {
2014     return GetClangType().IsIntegerType (is_signed);
2015 }
2016 
2017 bool
2018 ValueObject::IsPointerOrReferenceType ()
2019 {
2020     return GetClangType().IsPointerOrReferenceType ();
2021 }
2022 
2023 bool
2024 ValueObject::IsPossibleDynamicType ()
2025 {
2026     ExecutionContext exe_ctx (GetExecutionContextRef());
2027     Process *process = exe_ctx.GetProcessPtr();
2028     if (process)
2029         return process->IsPossibleDynamicValue(*this);
2030     else
2031         return GetClangType().IsPossibleDynamicType (NULL, true, true);
2032 }
2033 
2034 bool
2035 ValueObject::IsObjCNil ()
2036 {
2037     const uint32_t mask = ClangASTType::eTypeIsObjC | ClangASTType::eTypeIsPointer;
2038     bool isObjCpointer = (((GetClangType().GetTypeInfo(NULL)) & mask) == mask);
2039     if (!isObjCpointer)
2040         return false;
2041     bool canReadValue = true;
2042     bool isZero = GetValueAsUnsigned(0,&canReadValue) == 0;
2043     return canReadValue && isZero;
2044 }
2045 
2046 ValueObjectSP
2047 ValueObject::GetSyntheticArrayMember (size_t index, bool can_create)
2048 {
2049     const uint32_t type_info = GetTypeInfo ();
2050     if (type_info & ClangASTType::eTypeIsArray)
2051         return GetSyntheticArrayMemberFromArray(index, can_create);
2052 
2053     if (type_info & ClangASTType::eTypeIsPointer)
2054         return GetSyntheticArrayMemberFromPointer(index, can_create);
2055 
2056     return ValueObjectSP();
2057 
2058 }
2059 
2060 ValueObjectSP
2061 ValueObject::GetSyntheticArrayMemberFromPointer (size_t index, bool can_create)
2062 {
2063     ValueObjectSP synthetic_child_sp;
2064     if (IsPointerType ())
2065     {
2066         char index_str[64];
2067         snprintf(index_str, sizeof(index_str), "[%" PRIu64 "]", (uint64_t)index);
2068         ConstString index_const_str(index_str);
2069         // Check if we have already created a synthetic array member in this
2070         // valid object. If we have we will re-use it.
2071         synthetic_child_sp = GetSyntheticChild (index_const_str);
2072         if (!synthetic_child_sp)
2073         {
2074             ValueObject *synthetic_child;
2075             // We haven't made a synthetic array member for INDEX yet, so
2076             // lets make one and cache it for any future reference.
2077             synthetic_child = CreateChildAtIndex(0, true, index);
2078 
2079             // Cache the value if we got one back...
2080             if (synthetic_child)
2081             {
2082                 AddSyntheticChild(index_const_str, synthetic_child);
2083                 synthetic_child_sp = synthetic_child->GetSP();
2084                 synthetic_child_sp->SetName(ConstString(index_str));
2085                 synthetic_child_sp->m_is_array_item_for_pointer = true;
2086             }
2087         }
2088     }
2089     return synthetic_child_sp;
2090 }
2091 
2092 // This allows you to create an array member using and index
2093 // that doesn't not fall in the normal bounds of the array.
2094 // Many times structure can be defined as:
2095 // struct Collection
2096 // {
2097 //     uint32_t item_count;
2098 //     Item item_array[0];
2099 // };
2100 // The size of the "item_array" is 1, but many times in practice
2101 // there are more items in "item_array".
2102 
2103 ValueObjectSP
2104 ValueObject::GetSyntheticArrayMemberFromArray (size_t index, bool can_create)
2105 {
2106     ValueObjectSP synthetic_child_sp;
2107     if (IsArrayType ())
2108     {
2109         char index_str[64];
2110         snprintf(index_str, sizeof(index_str), "[%" PRIu64 "]", (uint64_t)index);
2111         ConstString index_const_str(index_str);
2112         // Check if we have already created a synthetic array member in this
2113         // valid object. If we have we will re-use it.
2114         synthetic_child_sp = GetSyntheticChild (index_const_str);
2115         if (!synthetic_child_sp)
2116         {
2117             ValueObject *synthetic_child;
2118             // We haven't made a synthetic array member for INDEX yet, so
2119             // lets make one and cache it for any future reference.
2120             synthetic_child = CreateChildAtIndex(0, true, index);
2121 
2122             // Cache the value if we got one back...
2123             if (synthetic_child)
2124             {
2125                 AddSyntheticChild(index_const_str, synthetic_child);
2126                 synthetic_child_sp = synthetic_child->GetSP();
2127                 synthetic_child_sp->SetName(ConstString(index_str));
2128                 synthetic_child_sp->m_is_array_item_for_pointer = true;
2129             }
2130         }
2131     }
2132     return synthetic_child_sp;
2133 }
2134 
2135 ValueObjectSP
2136 ValueObject::GetSyntheticBitFieldChild (uint32_t from, uint32_t to, bool can_create)
2137 {
2138     ValueObjectSP synthetic_child_sp;
2139     if (IsScalarType ())
2140     {
2141         char index_str[64];
2142         snprintf(index_str, sizeof(index_str), "[%i-%i]", from, to);
2143         ConstString index_const_str(index_str);
2144         // Check if we have already created a synthetic array member in this
2145         // valid object. If we have we will re-use it.
2146         synthetic_child_sp = GetSyntheticChild (index_const_str);
2147         if (!synthetic_child_sp)
2148         {
2149             // We haven't made a synthetic array member for INDEX yet, so
2150             // lets make one and cache it for any future reference.
2151             ValueObjectChild *synthetic_child = new ValueObjectChild (*this,
2152                                                                       GetClangType(),
2153                                                                       index_const_str,
2154                                                                       GetByteSize(),
2155                                                                       0,
2156                                                                       to-from+1,
2157                                                                       from,
2158                                                                       false,
2159                                                                       false,
2160                                                                       eAddressTypeInvalid);
2161 
2162             // Cache the value if we got one back...
2163             if (synthetic_child)
2164             {
2165                 AddSyntheticChild(index_const_str, synthetic_child);
2166                 synthetic_child_sp = synthetic_child->GetSP();
2167                 synthetic_child_sp->SetName(ConstString(index_str));
2168                 synthetic_child_sp->m_is_bitfield_for_scalar = true;
2169             }
2170         }
2171     }
2172     return synthetic_child_sp;
2173 }
2174 
2175 ValueObjectSP
2176 ValueObject::GetSyntheticChildAtOffset(uint32_t offset, const ClangASTType& type, bool can_create)
2177 {
2178 
2179     ValueObjectSP synthetic_child_sp;
2180 
2181     char name_str[64];
2182     snprintf(name_str, sizeof(name_str), "@%i", offset);
2183     ConstString name_const_str(name_str);
2184 
2185     // Check if we have already created a synthetic array member in this
2186     // valid object. If we have we will re-use it.
2187     synthetic_child_sp = GetSyntheticChild (name_const_str);
2188 
2189     if (synthetic_child_sp.get())
2190         return synthetic_child_sp;
2191 
2192     if (!can_create)
2193         return ValueObjectSP();
2194 
2195     ValueObjectChild *synthetic_child = new ValueObjectChild(*this,
2196                                                              type,
2197                                                              name_const_str,
2198                                                              type.GetByteSize(),
2199                                                              offset,
2200                                                              0,
2201                                                              0,
2202                                                              false,
2203                                                              false,
2204                                                              eAddressTypeInvalid);
2205     if (synthetic_child)
2206     {
2207         AddSyntheticChild(name_const_str, synthetic_child);
2208         synthetic_child_sp = synthetic_child->GetSP();
2209         synthetic_child_sp->SetName(name_const_str);
2210         synthetic_child_sp->m_is_child_at_offset = true;
2211     }
2212     return synthetic_child_sp;
2213 }
2214 
2215 // your expression path needs to have a leading . or ->
2216 // (unless it somehow "looks like" an array, in which case it has
2217 // a leading [ symbol). while the [ is meaningful and should be shown
2218 // to the user, . and -> are just parser design, but by no means
2219 // added information for the user.. strip them off
2220 static const char*
2221 SkipLeadingExpressionPathSeparators(const char* expression)
2222 {
2223     if (!expression || !expression[0])
2224         return expression;
2225     if (expression[0] == '.')
2226         return expression+1;
2227     if (expression[0] == '-' && expression[1] == '>')
2228         return expression+2;
2229     return expression;
2230 }
2231 
2232 ValueObjectSP
2233 ValueObject::GetSyntheticExpressionPathChild(const char* expression, bool can_create)
2234 {
2235     ValueObjectSP synthetic_child_sp;
2236     ConstString name_const_string(expression);
2237     // Check if we have already created a synthetic array member in this
2238     // valid object. If we have we will re-use it.
2239     synthetic_child_sp = GetSyntheticChild (name_const_string);
2240     if (!synthetic_child_sp)
2241     {
2242         // We haven't made a synthetic array member for expression yet, so
2243         // lets make one and cache it for any future reference.
2244         synthetic_child_sp = GetValueForExpressionPath(expression,
2245                                                        NULL, NULL, NULL,
2246                                                        GetValueForExpressionPathOptions().DontAllowSyntheticChildren());
2247 
2248         // Cache the value if we got one back...
2249         if (synthetic_child_sp.get())
2250         {
2251             // FIXME: this causes a "real" child to end up with its name changed to the contents of expression
2252             AddSyntheticChild(name_const_string, synthetic_child_sp.get());
2253             synthetic_child_sp->SetName(ConstString(SkipLeadingExpressionPathSeparators(expression)));
2254         }
2255     }
2256     return synthetic_child_sp;
2257 }
2258 
2259 void
2260 ValueObject::CalculateSyntheticValue (bool use_synthetic)
2261 {
2262     if (use_synthetic == false)
2263         return;
2264 
2265     TargetSP target_sp(GetTargetSP());
2266     if (target_sp && target_sp->GetEnableSyntheticValue() == false)
2267     {
2268         m_synthetic_value = NULL;
2269         return;
2270     }
2271 
2272     lldb::SyntheticChildrenSP current_synth_sp(m_synthetic_children_sp);
2273 
2274     if (!UpdateFormatsIfNeeded() && m_synthetic_value)
2275         return;
2276 
2277     if (m_synthetic_children_sp.get() == NULL)
2278         return;
2279 
2280     if (current_synth_sp == m_synthetic_children_sp && m_synthetic_value)
2281         return;
2282 
2283     m_synthetic_value = new ValueObjectSynthetic(*this, m_synthetic_children_sp);
2284 }
2285 
2286 void
2287 ValueObject::CalculateDynamicValue (DynamicValueType use_dynamic)
2288 {
2289     if (use_dynamic == eNoDynamicValues)
2290         return;
2291 
2292     if (!m_dynamic_value && !IsDynamic())
2293     {
2294         ExecutionContext exe_ctx (GetExecutionContextRef());
2295         Process *process = exe_ctx.GetProcessPtr();
2296         if (process && process->IsPossibleDynamicValue(*this))
2297         {
2298             ClearDynamicTypeInformation ();
2299             m_dynamic_value = new ValueObjectDynamicValue (*this, use_dynamic);
2300         }
2301     }
2302 }
2303 
2304 ValueObjectSP
2305 ValueObject::GetDynamicValue (DynamicValueType use_dynamic)
2306 {
2307     if (use_dynamic == eNoDynamicValues)
2308         return ValueObjectSP();
2309 
2310     if (!IsDynamic() && m_dynamic_value == NULL)
2311     {
2312         CalculateDynamicValue(use_dynamic);
2313     }
2314     if (m_dynamic_value)
2315         return m_dynamic_value->GetSP();
2316     else
2317         return ValueObjectSP();
2318 }
2319 
2320 ValueObjectSP
2321 ValueObject::GetStaticValue()
2322 {
2323     return GetSP();
2324 }
2325 
2326 lldb::ValueObjectSP
2327 ValueObject::GetNonSyntheticValue ()
2328 {
2329     return GetSP();
2330 }
2331 
2332 ValueObjectSP
2333 ValueObject::GetSyntheticValue (bool use_synthetic)
2334 {
2335     if (use_synthetic == false)
2336         return ValueObjectSP();
2337 
2338     CalculateSyntheticValue(use_synthetic);
2339 
2340     if (m_synthetic_value)
2341         return m_synthetic_value->GetSP();
2342     else
2343         return ValueObjectSP();
2344 }
2345 
2346 bool
2347 ValueObject::HasSyntheticValue()
2348 {
2349     UpdateFormatsIfNeeded();
2350 
2351     if (m_synthetic_children_sp.get() == NULL)
2352         return false;
2353 
2354     CalculateSyntheticValue(true);
2355 
2356     if (m_synthetic_value)
2357         return true;
2358     else
2359         return false;
2360 }
2361 
2362 bool
2363 ValueObject::GetBaseClassPath (Stream &s)
2364 {
2365     if (IsBaseClass())
2366     {
2367         bool parent_had_base_class = GetParent() && GetParent()->GetBaseClassPath (s);
2368         ClangASTType clang_type = GetClangType();
2369         std::string cxx_class_name;
2370         bool this_had_base_class = clang_type.GetCXXClassName (cxx_class_name);
2371         if (this_had_base_class)
2372         {
2373             if (parent_had_base_class)
2374                 s.PutCString("::");
2375             s.PutCString(cxx_class_name.c_str());
2376         }
2377         return parent_had_base_class || this_had_base_class;
2378     }
2379     return false;
2380 }
2381 
2382 
2383 ValueObject *
2384 ValueObject::GetNonBaseClassParent()
2385 {
2386     if (GetParent())
2387     {
2388         if (GetParent()->IsBaseClass())
2389             return GetParent()->GetNonBaseClassParent();
2390         else
2391             return GetParent();
2392     }
2393     return NULL;
2394 }
2395 
2396 void
2397 ValueObject::GetExpressionPath (Stream &s, bool qualify_cxx_base_classes, GetExpressionPathFormat epformat)
2398 {
2399     const bool is_deref_of_parent = IsDereferenceOfParent ();
2400 
2401     if (is_deref_of_parent && epformat == eGetExpressionPathFormatDereferencePointers)
2402     {
2403         // this is the original format of GetExpressionPath() producing code like *(a_ptr).memberName, which is entirely
2404         // fine, until you put this into StackFrame::GetValueForVariableExpressionPath() which prefers to see a_ptr->memberName.
2405         // the eHonorPointers mode is meant to produce strings in this latter format
2406         s.PutCString("*(");
2407     }
2408 
2409     ValueObject* parent = GetParent();
2410 
2411     if (parent)
2412         parent->GetExpressionPath (s, qualify_cxx_base_classes, epformat);
2413 
2414     // if we are a deref_of_parent just because we are synthetic array
2415     // members made up to allow ptr[%d] syntax to work in variable
2416     // printing, then add our name ([%d]) to the expression path
2417     if (m_is_array_item_for_pointer && epformat == eGetExpressionPathFormatHonorPointers)
2418         s.PutCString(m_name.AsCString());
2419 
2420     if (!IsBaseClass())
2421     {
2422         if (!is_deref_of_parent)
2423         {
2424             ValueObject *non_base_class_parent = GetNonBaseClassParent();
2425             if (non_base_class_parent)
2426             {
2427                 ClangASTType non_base_class_parent_clang_type = non_base_class_parent->GetClangType();
2428                 if (non_base_class_parent_clang_type)
2429                 {
2430                     if (parent && parent->IsDereferenceOfParent() && epformat == eGetExpressionPathFormatHonorPointers)
2431                     {
2432                         s.PutCString("->");
2433                     }
2434                     else
2435                     {
2436                         const uint32_t non_base_class_parent_type_info = non_base_class_parent_clang_type.GetTypeInfo();
2437 
2438                         if (non_base_class_parent_type_info & ClangASTType::eTypeIsPointer)
2439                         {
2440                             s.PutCString("->");
2441                         }
2442                         else if ((non_base_class_parent_type_info & ClangASTType::eTypeHasChildren) &&
2443                                  !(non_base_class_parent_type_info & ClangASTType::eTypeIsArray))
2444                         {
2445                             s.PutChar('.');
2446                         }
2447                     }
2448                 }
2449             }
2450 
2451             const char *name = GetName().GetCString();
2452             if (name)
2453             {
2454                 if (qualify_cxx_base_classes)
2455                 {
2456                     if (GetBaseClassPath (s))
2457                         s.PutCString("::");
2458                 }
2459                 s.PutCString(name);
2460             }
2461         }
2462     }
2463 
2464     if (is_deref_of_parent && epformat == eGetExpressionPathFormatDereferencePointers)
2465     {
2466         s.PutChar(')');
2467     }
2468 }
2469 
2470 ValueObjectSP
2471 ValueObject::GetValueForExpressionPath(const char* expression,
2472                                        const char** first_unparsed,
2473                                        ExpressionPathScanEndReason* reason_to_stop,
2474                                        ExpressionPathEndResultType* final_value_type,
2475                                        const GetValueForExpressionPathOptions& options,
2476                                        ExpressionPathAftermath* final_task_on_target)
2477 {
2478 
2479     const char* dummy_first_unparsed;
2480     ExpressionPathScanEndReason dummy_reason_to_stop = ValueObject::eExpressionPathScanEndReasonUnknown;
2481     ExpressionPathEndResultType dummy_final_value_type = ValueObject::eExpressionPathEndResultTypeInvalid;
2482     ExpressionPathAftermath dummy_final_task_on_target = ValueObject::eExpressionPathAftermathNothing;
2483 
2484     ValueObjectSP ret_val = GetValueForExpressionPath_Impl(expression,
2485                                                            first_unparsed ? first_unparsed : &dummy_first_unparsed,
2486                                                            reason_to_stop ? reason_to_stop : &dummy_reason_to_stop,
2487                                                            final_value_type ? final_value_type : &dummy_final_value_type,
2488                                                            options,
2489                                                            final_task_on_target ? final_task_on_target : &dummy_final_task_on_target);
2490 
2491     if (!final_task_on_target || *final_task_on_target == ValueObject::eExpressionPathAftermathNothing)
2492         return ret_val;
2493 
2494     if (ret_val.get() && ((final_value_type ? *final_value_type : dummy_final_value_type) == eExpressionPathEndResultTypePlain)) // I can only deref and takeaddress of plain objects
2495     {
2496         if ( (final_task_on_target ? *final_task_on_target : dummy_final_task_on_target) == ValueObject::eExpressionPathAftermathDereference)
2497         {
2498             Error error;
2499             ValueObjectSP final_value = ret_val->Dereference(error);
2500             if (error.Fail() || !final_value.get())
2501             {
2502                 if (reason_to_stop)
2503                     *reason_to_stop = ValueObject::eExpressionPathScanEndReasonDereferencingFailed;
2504                 if (final_value_type)
2505                     *final_value_type = ValueObject::eExpressionPathEndResultTypeInvalid;
2506                 return ValueObjectSP();
2507             }
2508             else
2509             {
2510                 if (final_task_on_target)
2511                     *final_task_on_target = ValueObject::eExpressionPathAftermathNothing;
2512                 return final_value;
2513             }
2514         }
2515         if (*final_task_on_target == ValueObject::eExpressionPathAftermathTakeAddress)
2516         {
2517             Error error;
2518             ValueObjectSP final_value = ret_val->AddressOf(error);
2519             if (error.Fail() || !final_value.get())
2520             {
2521                 if (reason_to_stop)
2522                     *reason_to_stop = ValueObject::eExpressionPathScanEndReasonTakingAddressFailed;
2523                 if (final_value_type)
2524                     *final_value_type = ValueObject::eExpressionPathEndResultTypeInvalid;
2525                 return ValueObjectSP();
2526             }
2527             else
2528             {
2529                 if (final_task_on_target)
2530                     *final_task_on_target = ValueObject::eExpressionPathAftermathNothing;
2531                 return final_value;
2532             }
2533         }
2534     }
2535     return ret_val; // final_task_on_target will still have its original value, so you know I did not do it
2536 }
2537 
2538 int
2539 ValueObject::GetValuesForExpressionPath(const char* expression,
2540                                         ValueObjectListSP& list,
2541                                         const char** first_unparsed,
2542                                         ExpressionPathScanEndReason* reason_to_stop,
2543                                         ExpressionPathEndResultType* final_value_type,
2544                                         const GetValueForExpressionPathOptions& options,
2545                                         ExpressionPathAftermath* final_task_on_target)
2546 {
2547     const char* dummy_first_unparsed;
2548     ExpressionPathScanEndReason dummy_reason_to_stop;
2549     ExpressionPathEndResultType dummy_final_value_type;
2550     ExpressionPathAftermath dummy_final_task_on_target = ValueObject::eExpressionPathAftermathNothing;
2551 
2552     ValueObjectSP ret_val = GetValueForExpressionPath_Impl(expression,
2553                                                            first_unparsed ? first_unparsed : &dummy_first_unparsed,
2554                                                            reason_to_stop ? reason_to_stop : &dummy_reason_to_stop,
2555                                                            final_value_type ? final_value_type : &dummy_final_value_type,
2556                                                            options,
2557                                                            final_task_on_target ? final_task_on_target : &dummy_final_task_on_target);
2558 
2559     if (!ret_val.get()) // if there are errors, I add nothing to the list
2560         return 0;
2561 
2562     if ( (reason_to_stop ? *reason_to_stop : dummy_reason_to_stop) != eExpressionPathScanEndReasonArrayRangeOperatorMet)
2563     {
2564         // I need not expand a range, just post-process the final value and return
2565         if (!final_task_on_target || *final_task_on_target == ValueObject::eExpressionPathAftermathNothing)
2566         {
2567             list->Append(ret_val);
2568             return 1;
2569         }
2570         if (ret_val.get() && (final_value_type ? *final_value_type : dummy_final_value_type) == eExpressionPathEndResultTypePlain) // I can only deref and takeaddress of plain objects
2571         {
2572             if (*final_task_on_target == ValueObject::eExpressionPathAftermathDereference)
2573             {
2574                 Error error;
2575                 ValueObjectSP final_value = ret_val->Dereference(error);
2576                 if (error.Fail() || !final_value.get())
2577                 {
2578                     if (reason_to_stop)
2579                         *reason_to_stop = ValueObject::eExpressionPathScanEndReasonDereferencingFailed;
2580                     if (final_value_type)
2581                         *final_value_type = ValueObject::eExpressionPathEndResultTypeInvalid;
2582                     return 0;
2583                 }
2584                 else
2585                 {
2586                     *final_task_on_target = ValueObject::eExpressionPathAftermathNothing;
2587                     list->Append(final_value);
2588                     return 1;
2589                 }
2590             }
2591             if (*final_task_on_target == ValueObject::eExpressionPathAftermathTakeAddress)
2592             {
2593                 Error error;
2594                 ValueObjectSP final_value = ret_val->AddressOf(error);
2595                 if (error.Fail() || !final_value.get())
2596                 {
2597                     if (reason_to_stop)
2598                         *reason_to_stop = ValueObject::eExpressionPathScanEndReasonTakingAddressFailed;
2599                     if (final_value_type)
2600                         *final_value_type = ValueObject::eExpressionPathEndResultTypeInvalid;
2601                     return 0;
2602                 }
2603                 else
2604                 {
2605                     *final_task_on_target = ValueObject::eExpressionPathAftermathNothing;
2606                     list->Append(final_value);
2607                     return 1;
2608                 }
2609             }
2610         }
2611     }
2612     else
2613     {
2614         return ExpandArraySliceExpression(first_unparsed ? *first_unparsed : dummy_first_unparsed,
2615                                           first_unparsed ? first_unparsed : &dummy_first_unparsed,
2616                                           ret_val,
2617                                           list,
2618                                           reason_to_stop ? reason_to_stop : &dummy_reason_to_stop,
2619                                           final_value_type ? final_value_type : &dummy_final_value_type,
2620                                           options,
2621                                           final_task_on_target ? final_task_on_target : &dummy_final_task_on_target);
2622     }
2623     // in any non-covered case, just do the obviously right thing
2624     list->Append(ret_val);
2625     return 1;
2626 }
2627 
2628 ValueObjectSP
2629 ValueObject::GetValueForExpressionPath_Impl(const char* expression_cstr,
2630                                             const char** first_unparsed,
2631                                             ExpressionPathScanEndReason* reason_to_stop,
2632                                             ExpressionPathEndResultType* final_result,
2633                                             const GetValueForExpressionPathOptions& options,
2634                                             ExpressionPathAftermath* what_next)
2635 {
2636     ValueObjectSP root = GetSP();
2637 
2638     if (!root.get())
2639         return ValueObjectSP();
2640 
2641     *first_unparsed = expression_cstr;
2642 
2643     while (true)
2644     {
2645 
2646         const char* expression_cstr = *first_unparsed; // hide the top level expression_cstr
2647 
2648         ClangASTType root_clang_type = root->GetClangType();
2649         ClangASTType pointee_clang_type;
2650         Flags pointee_clang_type_info;
2651 
2652         Flags root_clang_type_info(root_clang_type.GetTypeInfo(&pointee_clang_type));
2653         if (pointee_clang_type)
2654             pointee_clang_type_info.Reset(pointee_clang_type.GetTypeInfo());
2655 
2656         if (!expression_cstr || *expression_cstr == '\0')
2657         {
2658             *reason_to_stop = ValueObject::eExpressionPathScanEndReasonEndOfString;
2659             return root;
2660         }
2661 
2662         switch (*expression_cstr)
2663         {
2664             case '-':
2665             {
2666                 if (options.m_check_dot_vs_arrow_syntax &&
2667                     root_clang_type_info.Test(ClangASTType::eTypeIsPointer) ) // if you are trying to use -> on a non-pointer and I must catch the error
2668                 {
2669                     *first_unparsed = expression_cstr;
2670                     *reason_to_stop = ValueObject::eExpressionPathScanEndReasonArrowInsteadOfDot;
2671                     *final_result = ValueObject::eExpressionPathEndResultTypeInvalid;
2672                     return ValueObjectSP();
2673                 }
2674                 if (root_clang_type_info.Test(ClangASTType::eTypeIsObjC) &&  // if yo are trying to extract an ObjC IVar when this is forbidden
2675                     root_clang_type_info.Test(ClangASTType::eTypeIsPointer) &&
2676                     options.m_no_fragile_ivar)
2677                 {
2678                     *first_unparsed = expression_cstr;
2679                     *reason_to_stop = ValueObject::eExpressionPathScanEndReasonFragileIVarNotAllowed;
2680                     *final_result = ValueObject::eExpressionPathEndResultTypeInvalid;
2681                     return ValueObjectSP();
2682                 }
2683                 if (expression_cstr[1] != '>')
2684                 {
2685                     *first_unparsed = expression_cstr;
2686                     *reason_to_stop = ValueObject::eExpressionPathScanEndReasonUnexpectedSymbol;
2687                     *final_result = ValueObject::eExpressionPathEndResultTypeInvalid;
2688                     return ValueObjectSP();
2689                 }
2690                 expression_cstr++; // skip the -
2691             }
2692             case '.': // or fallthrough from ->
2693             {
2694                 if (options.m_check_dot_vs_arrow_syntax && *expression_cstr == '.' &&
2695                     root_clang_type_info.Test(ClangASTType::eTypeIsPointer)) // if you are trying to use . on a pointer and I must catch the error
2696                 {
2697                     *first_unparsed = expression_cstr;
2698                     *reason_to_stop = ValueObject::eExpressionPathScanEndReasonDotInsteadOfArrow;
2699                     *final_result = ValueObject::eExpressionPathEndResultTypeInvalid;
2700                     return ValueObjectSP();
2701                 }
2702                 expression_cstr++; // skip .
2703                 const char *next_separator = strpbrk(expression_cstr+1,"-.[");
2704                 ConstString child_name;
2705                 if (!next_separator) // if no other separator just expand this last layer
2706                 {
2707                     child_name.SetCString (expression_cstr);
2708                     ValueObjectSP child_valobj_sp = root->GetChildMemberWithName(child_name, true);
2709 
2710                     if (child_valobj_sp.get()) // we know we are done, so just return
2711                     {
2712                         *first_unparsed = "";
2713                         *reason_to_stop = ValueObject::eExpressionPathScanEndReasonEndOfString;
2714                         *final_result = ValueObject::eExpressionPathEndResultTypePlain;
2715                         return child_valobj_sp;
2716                     }
2717                     else if (options.m_no_synthetic_children == false) // let's try with synthetic children
2718                     {
2719                         if (root->IsSynthetic())
2720                         {
2721                             *first_unparsed = expression_cstr;
2722                             *reason_to_stop = ValueObject::eExpressionPathScanEndReasonNoSuchChild;
2723                             *final_result = ValueObject::eExpressionPathEndResultTypeInvalid;
2724                             return ValueObjectSP();
2725                         }
2726 
2727                         child_valobj_sp = root->GetSyntheticValue();
2728                         if (child_valobj_sp.get())
2729                             child_valobj_sp = child_valobj_sp->GetChildMemberWithName(child_name, true);
2730                     }
2731 
2732                     // if we are here and options.m_no_synthetic_children is true, child_valobj_sp is going to be a NULL SP,
2733                     // so we hit the "else" branch, and return an error
2734                     if(child_valobj_sp.get()) // if it worked, just return
2735                     {
2736                         *first_unparsed = "";
2737                         *reason_to_stop = ValueObject::eExpressionPathScanEndReasonEndOfString;
2738                         *final_result = ValueObject::eExpressionPathEndResultTypePlain;
2739                         return child_valobj_sp;
2740                     }
2741                     else
2742                     {
2743                         *first_unparsed = expression_cstr;
2744                         *reason_to_stop = ValueObject::eExpressionPathScanEndReasonNoSuchChild;
2745                         *final_result = ValueObject::eExpressionPathEndResultTypeInvalid;
2746                         return ValueObjectSP();
2747                     }
2748                 }
2749                 else // other layers do expand
2750                 {
2751                     child_name.SetCStringWithLength(expression_cstr, next_separator - expression_cstr);
2752                     ValueObjectSP child_valobj_sp = root->GetChildMemberWithName(child_name, true);
2753                     if (child_valobj_sp.get()) // store the new root and move on
2754                     {
2755                         root = child_valobj_sp;
2756                         *first_unparsed = next_separator;
2757                         *final_result = ValueObject::eExpressionPathEndResultTypePlain;
2758                         continue;
2759                     }
2760                     else if (options.m_no_synthetic_children == false) // let's try with synthetic children
2761                     {
2762                         if (root->IsSynthetic())
2763                         {
2764                             *first_unparsed = expression_cstr;
2765                             *reason_to_stop = ValueObject::eExpressionPathScanEndReasonNoSuchChild;
2766                             *final_result = ValueObject::eExpressionPathEndResultTypeInvalid;
2767                             return ValueObjectSP();
2768                         }
2769 
2770                         child_valobj_sp = root->GetSyntheticValue(true);
2771                         if (child_valobj_sp)
2772                             child_valobj_sp = child_valobj_sp->GetChildMemberWithName(child_name, true);
2773                     }
2774 
2775                     // if we are here and options.m_no_synthetic_children is true, child_valobj_sp is going to be a NULL SP,
2776                     // so we hit the "else" branch, and return an error
2777                     if(child_valobj_sp.get()) // if it worked, move on
2778                     {
2779                         root = child_valobj_sp;
2780                         *first_unparsed = next_separator;
2781                         *final_result = ValueObject::eExpressionPathEndResultTypePlain;
2782                         continue;
2783                     }
2784                     else
2785                     {
2786                         *first_unparsed = expression_cstr;
2787                         *reason_to_stop = ValueObject::eExpressionPathScanEndReasonNoSuchChild;
2788                         *final_result = ValueObject::eExpressionPathEndResultTypeInvalid;
2789                         return ValueObjectSP();
2790                     }
2791                 }
2792                 break;
2793             }
2794             case '[':
2795             {
2796                 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*
2797                 {
2798                     if (!root_clang_type_info.Test(ClangASTType::eTypeIsScalar)) // if this is not even a scalar...
2799                     {
2800                         if (options.m_no_synthetic_children) // ...only chance left is synthetic
2801                         {
2802                             *first_unparsed = expression_cstr;
2803                             *reason_to_stop = ValueObject::eExpressionPathScanEndReasonRangeOperatorInvalid;
2804                             *final_result = ValueObject::eExpressionPathEndResultTypeInvalid;
2805                             return ValueObjectSP();
2806                         }
2807                     }
2808                     else if (!options.m_allow_bitfields_syntax) // if this is a scalar, check that we can expand bitfields
2809                     {
2810                         *first_unparsed = expression_cstr;
2811                         *reason_to_stop = ValueObject::eExpressionPathScanEndReasonRangeOperatorNotAllowed;
2812                         *final_result = ValueObject::eExpressionPathEndResultTypeInvalid;
2813                         return ValueObjectSP();
2814                     }
2815                 }
2816                 if (*(expression_cstr+1) == ']') // if this is an unbounded range it only works for arrays
2817                 {
2818                     if (!root_clang_type_info.Test(ClangASTType::eTypeIsArray))
2819                     {
2820                         *first_unparsed = expression_cstr;
2821                         *reason_to_stop = ValueObject::eExpressionPathScanEndReasonEmptyRangeNotAllowed;
2822                         *final_result = ValueObject::eExpressionPathEndResultTypeInvalid;
2823                         return ValueObjectSP();
2824                     }
2825                     else // even if something follows, we cannot expand unbounded ranges, just let the caller do it
2826                     {
2827                         *first_unparsed = expression_cstr+2;
2828                         *reason_to_stop = ValueObject::eExpressionPathScanEndReasonArrayRangeOperatorMet;
2829                         *final_result = ValueObject::eExpressionPathEndResultTypeUnboundedRange;
2830                         return root;
2831                     }
2832                 }
2833                 const char *separator_position = ::strchr(expression_cstr+1,'-');
2834                 const char *close_bracket_position = ::strchr(expression_cstr+1,']');
2835                 if (!close_bracket_position) // if there is no ], this is a syntax error
2836                 {
2837                     *first_unparsed = expression_cstr;
2838                     *reason_to_stop = ValueObject::eExpressionPathScanEndReasonUnexpectedSymbol;
2839                     *final_result = ValueObject::eExpressionPathEndResultTypeInvalid;
2840                     return ValueObjectSP();
2841                 }
2842                 if (!separator_position || separator_position > close_bracket_position) // if no separator, this is either [] or [N]
2843                 {
2844                     char *end = NULL;
2845                     unsigned long index = ::strtoul (expression_cstr+1, &end, 0);
2846                     if (!end || end != close_bracket_position) // if something weird is in our way return an error
2847                     {
2848                         *first_unparsed = expression_cstr;
2849                         *reason_to_stop = ValueObject::eExpressionPathScanEndReasonUnexpectedSymbol;
2850                         *final_result = ValueObject::eExpressionPathEndResultTypeInvalid;
2851                         return ValueObjectSP();
2852                     }
2853                     if (end - expression_cstr == 1) // if this is [], only return a valid value for arrays
2854                     {
2855                         if (root_clang_type_info.Test(ClangASTType::eTypeIsArray))
2856                         {
2857                             *first_unparsed = expression_cstr+2;
2858                             *reason_to_stop = ValueObject::eExpressionPathScanEndReasonArrayRangeOperatorMet;
2859                             *final_result = ValueObject::eExpressionPathEndResultTypeUnboundedRange;
2860                             return root;
2861                         }
2862                         else
2863                         {
2864                             *first_unparsed = expression_cstr;
2865                             *reason_to_stop = ValueObject::eExpressionPathScanEndReasonEmptyRangeNotAllowed;
2866                             *final_result = ValueObject::eExpressionPathEndResultTypeInvalid;
2867                             return ValueObjectSP();
2868                         }
2869                     }
2870                     // from here on we do have a valid index
2871                     if (root_clang_type_info.Test(ClangASTType::eTypeIsArray))
2872                     {
2873                         ValueObjectSP child_valobj_sp = root->GetChildAtIndex(index, true);
2874                         if (!child_valobj_sp)
2875                             child_valobj_sp = root->GetSyntheticArrayMemberFromArray(index, true);
2876                         if (!child_valobj_sp)
2877                             if (root->HasSyntheticValue() && root->GetSyntheticValue()->GetNumChildren() > index)
2878                                 child_valobj_sp = root->GetSyntheticValue()->GetChildAtIndex(index, true);
2879                         if (child_valobj_sp)
2880                         {
2881                             root = child_valobj_sp;
2882                             *first_unparsed = end+1; // skip ]
2883                             *final_result = ValueObject::eExpressionPathEndResultTypePlain;
2884                             continue;
2885                         }
2886                         else
2887                         {
2888                             *first_unparsed = expression_cstr;
2889                             *reason_to_stop = ValueObject::eExpressionPathScanEndReasonNoSuchChild;
2890                             *final_result = ValueObject::eExpressionPathEndResultTypeInvalid;
2891                             return ValueObjectSP();
2892                         }
2893                     }
2894                     else if (root_clang_type_info.Test(ClangASTType::eTypeIsPointer))
2895                     {
2896                         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
2897                             pointee_clang_type_info.Test(ClangASTType::eTypeIsScalar))
2898                         {
2899                             Error error;
2900                             root = root->Dereference(error);
2901                             if (error.Fail() || !root.get())
2902                             {
2903                                 *first_unparsed = expression_cstr;
2904                                 *reason_to_stop = ValueObject::eExpressionPathScanEndReasonDereferencingFailed;
2905                                 *final_result = ValueObject::eExpressionPathEndResultTypeInvalid;
2906                                 return ValueObjectSP();
2907                             }
2908                             else
2909                             {
2910                                 *what_next = eExpressionPathAftermathNothing;
2911                                 continue;
2912                             }
2913                         }
2914                         else
2915                         {
2916                             if (root->GetClangType().GetMinimumLanguage() == eLanguageTypeObjC
2917                                 && pointee_clang_type_info.AllClear(ClangASTType::eTypeIsPointer)
2918                                 && root->HasSyntheticValue()
2919                                 && options.m_no_synthetic_children == false)
2920                             {
2921                                 root = root->GetSyntheticValue()->GetChildAtIndex(index, true);
2922                             }
2923                             else
2924                                 root = root->GetSyntheticArrayMemberFromPointer(index, true);
2925                             if (!root.get())
2926                             {
2927                                 *first_unparsed = expression_cstr;
2928                                 *reason_to_stop = ValueObject::eExpressionPathScanEndReasonNoSuchChild;
2929                                 *final_result = ValueObject::eExpressionPathEndResultTypeInvalid;
2930                                 return ValueObjectSP();
2931                             }
2932                             else
2933                             {
2934                                 *first_unparsed = end+1; // skip ]
2935                                 *final_result = ValueObject::eExpressionPathEndResultTypePlain;
2936                                 continue;
2937                             }
2938                         }
2939                     }
2940                     else if (root_clang_type_info.Test(ClangASTType::eTypeIsScalar))
2941                     {
2942                         root = root->GetSyntheticBitFieldChild(index, index, true);
2943                         if (!root.get())
2944                         {
2945                             *first_unparsed = expression_cstr;
2946                             *reason_to_stop = ValueObject::eExpressionPathScanEndReasonNoSuchChild;
2947                             *final_result = ValueObject::eExpressionPathEndResultTypeInvalid;
2948                             return ValueObjectSP();
2949                         }
2950                         else // we do not know how to expand members of bitfields, so we just return and let the caller do any further processing
2951                         {
2952                             *first_unparsed = end+1; // skip ]
2953                             *reason_to_stop = ValueObject::eExpressionPathScanEndReasonBitfieldRangeOperatorMet;
2954                             *final_result = ValueObject::eExpressionPathEndResultTypeBitfield;
2955                             return root;
2956                         }
2957                     }
2958                     else if (root_clang_type_info.Test(ClangASTType::eTypeIsVector))
2959                     {
2960                         root = root->GetChildAtIndex(index, true);
2961                         if (!root.get())
2962                         {
2963                             *first_unparsed = expression_cstr;
2964                             *reason_to_stop = ValueObject::eExpressionPathScanEndReasonNoSuchChild;
2965                             *final_result = ValueObject::eExpressionPathEndResultTypeInvalid;
2966                             return ValueObjectSP();
2967                         }
2968                         else
2969                         {
2970                             *first_unparsed = end+1; // skip ]
2971                             *final_result = ValueObject::eExpressionPathEndResultTypePlain;
2972                             continue;
2973                         }
2974                     }
2975                     else if (options.m_no_synthetic_children == false)
2976                     {
2977                         if (root->HasSyntheticValue())
2978                             root = root->GetSyntheticValue();
2979                         else if (!root->IsSynthetic())
2980                         {
2981                             *first_unparsed = expression_cstr;
2982                             *reason_to_stop = ValueObject::eExpressionPathScanEndReasonSyntheticValueMissing;
2983                             *final_result = ValueObject::eExpressionPathEndResultTypeInvalid;
2984                             return ValueObjectSP();
2985                         }
2986                         // if we are here, then root itself is a synthetic VO.. should be good to go
2987 
2988                         if (!root.get())
2989                         {
2990                             *first_unparsed = expression_cstr;
2991                             *reason_to_stop = ValueObject::eExpressionPathScanEndReasonSyntheticValueMissing;
2992                             *final_result = ValueObject::eExpressionPathEndResultTypeInvalid;
2993                             return ValueObjectSP();
2994                         }
2995                         root = root->GetChildAtIndex(index, true);
2996                         if (!root.get())
2997                         {
2998                             *first_unparsed = expression_cstr;
2999                             *reason_to_stop = ValueObject::eExpressionPathScanEndReasonNoSuchChild;
3000                             *final_result = ValueObject::eExpressionPathEndResultTypeInvalid;
3001                             return ValueObjectSP();
3002                         }
3003                         else
3004                         {
3005                             *first_unparsed = end+1; // skip ]
3006                             *final_result = ValueObject::eExpressionPathEndResultTypePlain;
3007                             continue;
3008                         }
3009                     }
3010                     else
3011                     {
3012                         *first_unparsed = expression_cstr;
3013                         *reason_to_stop = ValueObject::eExpressionPathScanEndReasonNoSuchChild;
3014                         *final_result = ValueObject::eExpressionPathEndResultTypeInvalid;
3015                         return ValueObjectSP();
3016                     }
3017                 }
3018                 else // we have a low and a high index
3019                 {
3020                     char *end = NULL;
3021                     unsigned long index_lower = ::strtoul (expression_cstr+1, &end, 0);
3022                     if (!end || end != separator_position) // if something weird is in our way return an error
3023                     {
3024                         *first_unparsed = expression_cstr;
3025                         *reason_to_stop = ValueObject::eExpressionPathScanEndReasonUnexpectedSymbol;
3026                         *final_result = ValueObject::eExpressionPathEndResultTypeInvalid;
3027                         return ValueObjectSP();
3028                     }
3029                     unsigned long index_higher = ::strtoul (separator_position+1, &end, 0);
3030                     if (!end || end != close_bracket_position) // if something weird is in our way return an error
3031                     {
3032                         *first_unparsed = expression_cstr;
3033                         *reason_to_stop = ValueObject::eExpressionPathScanEndReasonUnexpectedSymbol;
3034                         *final_result = ValueObject::eExpressionPathEndResultTypeInvalid;
3035                         return ValueObjectSP();
3036                     }
3037                     if (index_lower > index_higher) // swap indices if required
3038                     {
3039                         unsigned long temp = index_lower;
3040                         index_lower = index_higher;
3041                         index_higher = temp;
3042                     }
3043                     if (root_clang_type_info.Test(ClangASTType::eTypeIsScalar)) // expansion only works for scalars
3044                     {
3045                         root = root->GetSyntheticBitFieldChild(index_lower, index_higher, true);
3046                         if (!root.get())
3047                         {
3048                             *first_unparsed = expression_cstr;
3049                             *reason_to_stop = ValueObject::eExpressionPathScanEndReasonNoSuchChild;
3050                             *final_result = ValueObject::eExpressionPathEndResultTypeInvalid;
3051                             return ValueObjectSP();
3052                         }
3053                         else
3054                         {
3055                             *first_unparsed = end+1; // skip ]
3056                             *reason_to_stop = ValueObject::eExpressionPathScanEndReasonBitfieldRangeOperatorMet;
3057                             *final_result = ValueObject::eExpressionPathEndResultTypeBitfield;
3058                             return root;
3059                         }
3060                     }
3061                     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
3062                              *what_next == ValueObject::eExpressionPathAftermathDereference &&
3063                              pointee_clang_type_info.Test(ClangASTType::eTypeIsScalar))
3064                     {
3065                         Error error;
3066                         root = root->Dereference(error);
3067                         if (error.Fail() || !root.get())
3068                         {
3069                             *first_unparsed = expression_cstr;
3070                             *reason_to_stop = ValueObject::eExpressionPathScanEndReasonDereferencingFailed;
3071                             *final_result = ValueObject::eExpressionPathEndResultTypeInvalid;
3072                             return ValueObjectSP();
3073                         }
3074                         else
3075                         {
3076                             *what_next = ValueObject::eExpressionPathAftermathNothing;
3077                             continue;
3078                         }
3079                     }
3080                     else
3081                     {
3082                         *first_unparsed = expression_cstr;
3083                         *reason_to_stop = ValueObject::eExpressionPathScanEndReasonArrayRangeOperatorMet;
3084                         *final_result = ValueObject::eExpressionPathEndResultTypeBoundedRange;
3085                         return root;
3086                     }
3087                 }
3088                 break;
3089             }
3090             default: // some non-separator is in the way
3091             {
3092                 *first_unparsed = expression_cstr;
3093                 *reason_to_stop = ValueObject::eExpressionPathScanEndReasonUnexpectedSymbol;
3094                 *final_result = ValueObject::eExpressionPathEndResultTypeInvalid;
3095                 return ValueObjectSP();
3096                 break;
3097             }
3098         }
3099     }
3100 }
3101 
3102 int
3103 ValueObject::ExpandArraySliceExpression(const char* expression_cstr,
3104                                         const char** first_unparsed,
3105                                         ValueObjectSP root,
3106                                         ValueObjectListSP& list,
3107                                         ExpressionPathScanEndReason* reason_to_stop,
3108                                         ExpressionPathEndResultType* final_result,
3109                                         const GetValueForExpressionPathOptions& options,
3110                                         ExpressionPathAftermath* what_next)
3111 {
3112     if (!root.get())
3113         return 0;
3114 
3115     *first_unparsed = expression_cstr;
3116 
3117     while (true)
3118     {
3119 
3120         const char* expression_cstr = *first_unparsed; // hide the top level expression_cstr
3121 
3122         ClangASTType root_clang_type = root->GetClangType();
3123         ClangASTType pointee_clang_type;
3124         Flags pointee_clang_type_info;
3125         Flags root_clang_type_info(root_clang_type.GetTypeInfo(&pointee_clang_type));
3126         if (pointee_clang_type)
3127             pointee_clang_type_info.Reset(pointee_clang_type.GetTypeInfo());
3128 
3129         if (!expression_cstr || *expression_cstr == '\0')
3130         {
3131             *reason_to_stop = ValueObject::eExpressionPathScanEndReasonEndOfString;
3132             list->Append(root);
3133             return 1;
3134         }
3135 
3136         switch (*expression_cstr)
3137         {
3138             case '[':
3139             {
3140                 if (!root_clang_type_info.Test(ClangASTType::eTypeIsArray) && !root_clang_type_info.Test(ClangASTType::eTypeIsPointer)) // if this is not a T[] nor a T*
3141                 {
3142                     if (!root_clang_type_info.Test(ClangASTType::eTypeIsScalar)) // if this is not even a scalar, this syntax is just plain wrong!
3143                     {
3144                         *first_unparsed = expression_cstr;
3145                         *reason_to_stop = ValueObject::eExpressionPathScanEndReasonRangeOperatorInvalid;
3146                         *final_result = ValueObject::eExpressionPathEndResultTypeInvalid;
3147                         return 0;
3148                     }
3149                     else if (!options.m_allow_bitfields_syntax) // if this is a scalar, check that we can expand bitfields
3150                     {
3151                         *first_unparsed = expression_cstr;
3152                         *reason_to_stop = ValueObject::eExpressionPathScanEndReasonRangeOperatorNotAllowed;
3153                         *final_result = ValueObject::eExpressionPathEndResultTypeInvalid;
3154                         return 0;
3155                     }
3156                 }
3157                 if (*(expression_cstr+1) == ']') // if this is an unbounded range it only works for arrays
3158                 {
3159                     if (!root_clang_type_info.Test(ClangASTType::eTypeIsArray))
3160                     {
3161                         *first_unparsed = expression_cstr;
3162                         *reason_to_stop = ValueObject::eExpressionPathScanEndReasonEmptyRangeNotAllowed;
3163                         *final_result = ValueObject::eExpressionPathEndResultTypeInvalid;
3164                         return 0;
3165                     }
3166                     else // expand this into list
3167                     {
3168                         const size_t max_index = root->GetNumChildren() - 1;
3169                         for (size_t index = 0; index < max_index; index++)
3170                         {
3171                             ValueObjectSP child =
3172                                 root->GetChildAtIndex(index, true);
3173                             list->Append(child);
3174                         }
3175                         *first_unparsed = expression_cstr+2;
3176                         *reason_to_stop = ValueObject::eExpressionPathScanEndReasonRangeOperatorExpanded;
3177                         *final_result = ValueObject::eExpressionPathEndResultTypeValueObjectList;
3178                         return max_index; // tell me number of items I added to the VOList
3179                     }
3180                 }
3181                 const char *separator_position = ::strchr(expression_cstr+1,'-');
3182                 const char *close_bracket_position = ::strchr(expression_cstr+1,']');
3183                 if (!close_bracket_position) // if there is no ], this is a syntax error
3184                 {
3185                     *first_unparsed = expression_cstr;
3186                     *reason_to_stop = ValueObject::eExpressionPathScanEndReasonUnexpectedSymbol;
3187                     *final_result = ValueObject::eExpressionPathEndResultTypeInvalid;
3188                     return 0;
3189                 }
3190                 if (!separator_position || separator_position > close_bracket_position) // if no separator, this is either [] or [N]
3191                 {
3192                     char *end = NULL;
3193                     unsigned long index = ::strtoul (expression_cstr+1, &end, 0);
3194                     if (!end || end != close_bracket_position) // if something weird is in our way return an error
3195                     {
3196                         *first_unparsed = expression_cstr;
3197                         *reason_to_stop = ValueObject::eExpressionPathScanEndReasonUnexpectedSymbol;
3198                         *final_result = ValueObject::eExpressionPathEndResultTypeInvalid;
3199                         return 0;
3200                     }
3201                     if (end - expression_cstr == 1) // if this is [], only return a valid value for arrays
3202                     {
3203                         if (root_clang_type_info.Test(ClangASTType::eTypeIsArray))
3204                         {
3205                             const size_t max_index = root->GetNumChildren() - 1;
3206                             for (size_t index = 0; index < max_index; index++)
3207                             {
3208                                 ValueObjectSP child =
3209                                 root->GetChildAtIndex(index, true);
3210                                 list->Append(child);
3211                             }
3212                             *first_unparsed = expression_cstr+2;
3213                             *reason_to_stop = ValueObject::eExpressionPathScanEndReasonRangeOperatorExpanded;
3214                             *final_result = ValueObject::eExpressionPathEndResultTypeValueObjectList;
3215                             return max_index; // tell me number of items I added to the VOList
3216                         }
3217                         else
3218                         {
3219                             *first_unparsed = expression_cstr;
3220                             *reason_to_stop = ValueObject::eExpressionPathScanEndReasonEmptyRangeNotAllowed;
3221                             *final_result = ValueObject::eExpressionPathEndResultTypeInvalid;
3222                             return 0;
3223                         }
3224                     }
3225                     // from here on we do have a valid index
3226                     if (root_clang_type_info.Test(ClangASTType::eTypeIsArray))
3227                     {
3228                         root = root->GetChildAtIndex(index, true);
3229                         if (!root.get())
3230                         {
3231                             *first_unparsed = expression_cstr;
3232                             *reason_to_stop = ValueObject::eExpressionPathScanEndReasonNoSuchChild;
3233                             *final_result = ValueObject::eExpressionPathEndResultTypeInvalid;
3234                             return 0;
3235                         }
3236                         else
3237                         {
3238                             list->Append(root);
3239                             *first_unparsed = end+1; // skip ]
3240                             *reason_to_stop = ValueObject::eExpressionPathScanEndReasonRangeOperatorExpanded;
3241                             *final_result = ValueObject::eExpressionPathEndResultTypeValueObjectList;
3242                             return 1;
3243                         }
3244                     }
3245                     else if (root_clang_type_info.Test(ClangASTType::eTypeIsPointer))
3246                     {
3247                         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
3248                             pointee_clang_type_info.Test(ClangASTType::eTypeIsScalar))
3249                         {
3250                             Error error;
3251                             root = root->Dereference(error);
3252                             if (error.Fail() || !root.get())
3253                             {
3254                                 *first_unparsed = expression_cstr;
3255                                 *reason_to_stop = ValueObject::eExpressionPathScanEndReasonDereferencingFailed;
3256                                 *final_result = ValueObject::eExpressionPathEndResultTypeInvalid;
3257                                 return 0;
3258                             }
3259                             else
3260                             {
3261                                 *what_next = eExpressionPathAftermathNothing;
3262                                 continue;
3263                             }
3264                         }
3265                         else
3266                         {
3267                             root = root->GetSyntheticArrayMemberFromPointer(index, true);
3268                             if (!root.get())
3269                             {
3270                                 *first_unparsed = expression_cstr;
3271                                 *reason_to_stop = ValueObject::eExpressionPathScanEndReasonNoSuchChild;
3272                                 *final_result = ValueObject::eExpressionPathEndResultTypeInvalid;
3273                                 return 0;
3274                             }
3275                             else
3276                             {
3277                                 list->Append(root);
3278                                 *first_unparsed = end+1; // skip ]
3279                                 *reason_to_stop = ValueObject::eExpressionPathScanEndReasonRangeOperatorExpanded;
3280                                 *final_result = ValueObject::eExpressionPathEndResultTypeValueObjectList;
3281                                 return 1;
3282                             }
3283                         }
3284                     }
3285                     else /*if (ClangASTContext::IsScalarType(root_clang_type))*/
3286                     {
3287                         root = root->GetSyntheticBitFieldChild(index, index, true);
3288                         if (!root.get())
3289                         {
3290                             *first_unparsed = expression_cstr;
3291                             *reason_to_stop = ValueObject::eExpressionPathScanEndReasonNoSuchChild;
3292                             *final_result = ValueObject::eExpressionPathEndResultTypeInvalid;
3293                             return 0;
3294                         }
3295                         else // we do not know how to expand members of bitfields, so we just return and let the caller do any further processing
3296                         {
3297                             list->Append(root);
3298                             *first_unparsed = end+1; // skip ]
3299                             *reason_to_stop = ValueObject::eExpressionPathScanEndReasonRangeOperatorExpanded;
3300                             *final_result = ValueObject::eExpressionPathEndResultTypeValueObjectList;
3301                             return 1;
3302                         }
3303                     }
3304                 }
3305                 else // we have a low and a high index
3306                 {
3307                     char *end = NULL;
3308                     unsigned long index_lower = ::strtoul (expression_cstr+1, &end, 0);
3309                     if (!end || end != separator_position) // if something weird is in our way return an error
3310                     {
3311                         *first_unparsed = expression_cstr;
3312                         *reason_to_stop = ValueObject::eExpressionPathScanEndReasonUnexpectedSymbol;
3313                         *final_result = ValueObject::eExpressionPathEndResultTypeInvalid;
3314                         return 0;
3315                     }
3316                     unsigned long index_higher = ::strtoul (separator_position+1, &end, 0);
3317                     if (!end || end != close_bracket_position) // if something weird is in our way return an error
3318                     {
3319                         *first_unparsed = expression_cstr;
3320                         *reason_to_stop = ValueObject::eExpressionPathScanEndReasonUnexpectedSymbol;
3321                         *final_result = ValueObject::eExpressionPathEndResultTypeInvalid;
3322                         return 0;
3323                     }
3324                     if (index_lower > index_higher) // swap indices if required
3325                     {
3326                         unsigned long temp = index_lower;
3327                         index_lower = index_higher;
3328                         index_higher = temp;
3329                     }
3330                     if (root_clang_type_info.Test(ClangASTType::eTypeIsScalar)) // expansion only works for scalars
3331                     {
3332                         root = root->GetSyntheticBitFieldChild(index_lower, index_higher, true);
3333                         if (!root.get())
3334                         {
3335                             *first_unparsed = expression_cstr;
3336                             *reason_to_stop = ValueObject::eExpressionPathScanEndReasonNoSuchChild;
3337                             *final_result = ValueObject::eExpressionPathEndResultTypeInvalid;
3338                             return 0;
3339                         }
3340                         else
3341                         {
3342                             list->Append(root);
3343                             *first_unparsed = end+1; // skip ]
3344                             *reason_to_stop = ValueObject::eExpressionPathScanEndReasonRangeOperatorExpanded;
3345                             *final_result = ValueObject::eExpressionPathEndResultTypeValueObjectList;
3346                             return 1;
3347                         }
3348                     }
3349                     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
3350                              *what_next == ValueObject::eExpressionPathAftermathDereference &&
3351                              pointee_clang_type_info.Test(ClangASTType::eTypeIsScalar))
3352                     {
3353                         Error error;
3354                         root = root->Dereference(error);
3355                         if (error.Fail() || !root.get())
3356                         {
3357                             *first_unparsed = expression_cstr;
3358                             *reason_to_stop = ValueObject::eExpressionPathScanEndReasonDereferencingFailed;
3359                             *final_result = ValueObject::eExpressionPathEndResultTypeInvalid;
3360                             return 0;
3361                         }
3362                         else
3363                         {
3364                             *what_next = ValueObject::eExpressionPathAftermathNothing;
3365                             continue;
3366                         }
3367                     }
3368                     else
3369                     {
3370                         for (unsigned long index = index_lower;
3371                              index <= index_higher; index++)
3372                         {
3373                             ValueObjectSP child =
3374                                 root->GetChildAtIndex(index, true);
3375                             list->Append(child);
3376                         }
3377                         *first_unparsed = end+1;
3378                         *reason_to_stop = ValueObject::eExpressionPathScanEndReasonRangeOperatorExpanded;
3379                         *final_result = ValueObject::eExpressionPathEndResultTypeValueObjectList;
3380                         return index_higher-index_lower+1; // tell me number of items I added to the VOList
3381                     }
3382                 }
3383                 break;
3384             }
3385             default: // some non-[ separator, or something entirely wrong, is in the way
3386             {
3387                 *first_unparsed = expression_cstr;
3388                 *reason_to_stop = ValueObject::eExpressionPathScanEndReasonUnexpectedSymbol;
3389                 *final_result = ValueObject::eExpressionPathEndResultTypeInvalid;
3390                 return 0;
3391                 break;
3392             }
3393         }
3394     }
3395 }
3396 
3397 void
3398 ValueObject::LogValueObject (Log *log)
3399 {
3400     if (log)
3401         return LogValueObject (log, DumpValueObjectOptions::DefaultOptions());
3402 }
3403 
3404 void
3405 ValueObject::LogValueObject (Log *log, const DumpValueObjectOptions& options)
3406 {
3407     if (log)
3408     {
3409         StreamString s;
3410         Dump (s, options);
3411         if (s.GetSize())
3412             log->PutCString(s.GetData());
3413     }
3414 }
3415 
3416 void
3417 ValueObject::Dump (Stream &s)
3418 {
3419 
3420     ValueObjectPrinter printer(this,&s,DumpValueObjectOptions::DefaultOptions());
3421     printer.PrintValueObject();
3422 }
3423 
3424 void
3425 ValueObject::Dump (Stream &s,
3426                    const DumpValueObjectOptions& options)
3427 {
3428     ValueObjectPrinter printer(this,&s,options);
3429     printer.PrintValueObject();
3430 }
3431 
3432 ValueObjectSP
3433 ValueObject::CreateConstantValue (const ConstString &name)
3434 {
3435     ValueObjectSP valobj_sp;
3436 
3437     if (UpdateValueIfNeeded(false) && m_error.Success())
3438     {
3439         ExecutionContext exe_ctx (GetExecutionContextRef());
3440 
3441         DataExtractor data;
3442         data.SetByteOrder (m_data.GetByteOrder());
3443         data.SetAddressByteSize(m_data.GetAddressByteSize());
3444 
3445         if (IsBitfield())
3446         {
3447             Value v(Scalar(GetValueAsUnsigned(UINT64_MAX)));
3448             m_error = v.GetValueAsData (&exe_ctx, data, 0, GetModule().get());
3449         }
3450         else
3451             m_error = m_value.GetValueAsData (&exe_ctx, data, 0, GetModule().get());
3452 
3453         valobj_sp = ValueObjectConstResult::Create (exe_ctx.GetBestExecutionContextScope(),
3454                                                     GetClangType(),
3455                                                     name,
3456                                                     data,
3457                                                     GetAddressOf());
3458     }
3459 
3460     if (!valobj_sp)
3461     {
3462         ExecutionContext exe_ctx (GetExecutionContextRef());
3463         valobj_sp = ValueObjectConstResult::Create (exe_ctx.GetBestExecutionContextScope(), m_error);
3464     }
3465     return valobj_sp;
3466 }
3467 
3468 ValueObjectSP
3469 ValueObject::Dereference (Error &error)
3470 {
3471     if (m_deref_valobj)
3472         return m_deref_valobj->GetSP();
3473 
3474     const bool is_pointer_type = IsPointerType();
3475     if (is_pointer_type)
3476     {
3477         bool omit_empty_base_classes = true;
3478         bool ignore_array_bounds = false;
3479 
3480         std::string child_name_str;
3481         uint32_t child_byte_size = 0;
3482         int32_t child_byte_offset = 0;
3483         uint32_t child_bitfield_bit_size = 0;
3484         uint32_t child_bitfield_bit_offset = 0;
3485         bool child_is_base_class = false;
3486         bool child_is_deref_of_parent = false;
3487         const bool transparent_pointers = false;
3488         ClangASTType clang_type = GetClangType();
3489         ClangASTType child_clang_type;
3490 
3491         ExecutionContext exe_ctx (GetExecutionContextRef());
3492 
3493         child_clang_type = clang_type.GetChildClangTypeAtIndex (&exe_ctx,
3494                                                                 GetName().GetCString(),
3495                                                                 0,
3496                                                                 transparent_pointers,
3497                                                                 omit_empty_base_classes,
3498                                                                 ignore_array_bounds,
3499                                                                 child_name_str,
3500                                                                 child_byte_size,
3501                                                                 child_byte_offset,
3502                                                                 child_bitfield_bit_size,
3503                                                                 child_bitfield_bit_offset,
3504                                                                 child_is_base_class,
3505                                                                 child_is_deref_of_parent);
3506         if (child_clang_type && child_byte_size)
3507         {
3508             ConstString child_name;
3509             if (!child_name_str.empty())
3510                 child_name.SetCString (child_name_str.c_str());
3511 
3512             m_deref_valobj = new ValueObjectChild (*this,
3513                                                    child_clang_type,
3514                                                    child_name,
3515                                                    child_byte_size,
3516                                                    child_byte_offset,
3517                                                    child_bitfield_bit_size,
3518                                                    child_bitfield_bit_offset,
3519                                                    child_is_base_class,
3520                                                    child_is_deref_of_parent,
3521                                                    eAddressTypeInvalid);
3522         }
3523     }
3524 
3525     if (m_deref_valobj)
3526     {
3527         error.Clear();
3528         return m_deref_valobj->GetSP();
3529     }
3530     else
3531     {
3532         StreamString strm;
3533         GetExpressionPath(strm, true);
3534 
3535         if (is_pointer_type)
3536             error.SetErrorStringWithFormat("dereference failed: (%s) %s", GetTypeName().AsCString("<invalid type>"), strm.GetString().c_str());
3537         else
3538             error.SetErrorStringWithFormat("not a pointer type: (%s) %s", GetTypeName().AsCString("<invalid type>"), strm.GetString().c_str());
3539         return ValueObjectSP();
3540     }
3541 }
3542 
3543 ValueObjectSP
3544 ValueObject::AddressOf (Error &error)
3545 {
3546     if (m_addr_of_valobj_sp)
3547         return m_addr_of_valobj_sp;
3548 
3549     AddressType address_type = eAddressTypeInvalid;
3550     const bool scalar_is_load_address = false;
3551     addr_t addr = GetAddressOf (scalar_is_load_address, &address_type);
3552     error.Clear();
3553     if (addr != LLDB_INVALID_ADDRESS)
3554     {
3555         switch (address_type)
3556         {
3557         case eAddressTypeInvalid:
3558             {
3559                 StreamString expr_path_strm;
3560                 GetExpressionPath(expr_path_strm, true);
3561                 error.SetErrorStringWithFormat("'%s' is not in memory", expr_path_strm.GetString().c_str());
3562             }
3563             break;
3564 
3565         case eAddressTypeFile:
3566         case eAddressTypeLoad:
3567         case eAddressTypeHost:
3568             {
3569                 ClangASTType clang_type = GetClangType();
3570                 if (clang_type)
3571                 {
3572                     std::string name (1, '&');
3573                     name.append (m_name.AsCString(""));
3574                     ExecutionContext exe_ctx (GetExecutionContextRef());
3575                     m_addr_of_valobj_sp = ValueObjectConstResult::Create (exe_ctx.GetBestExecutionContextScope(),
3576                                                                           clang_type.GetPointerType(),
3577                                                                           ConstString (name.c_str()),
3578                                                                           addr,
3579                                                                           eAddressTypeInvalid,
3580                                                                           m_data.GetAddressByteSize());
3581                 }
3582             }
3583             break;
3584         }
3585     }
3586     else
3587     {
3588         StreamString expr_path_strm;
3589         GetExpressionPath(expr_path_strm, true);
3590         error.SetErrorStringWithFormat("'%s' doesn't have a valid address", expr_path_strm.GetString().c_str());
3591     }
3592 
3593     return m_addr_of_valobj_sp;
3594 }
3595 
3596 ValueObjectSP
3597 ValueObject::Cast (const ClangASTType &clang_ast_type)
3598 {
3599     return ValueObjectCast::Create (*this, GetName(), clang_ast_type);
3600 }
3601 
3602 ValueObjectSP
3603 ValueObject::CastPointerType (const char *name, ClangASTType &clang_ast_type)
3604 {
3605     ValueObjectSP valobj_sp;
3606     AddressType address_type;
3607     addr_t ptr_value = GetPointerValue (&address_type);
3608 
3609     if (ptr_value != LLDB_INVALID_ADDRESS)
3610     {
3611         Address ptr_addr (ptr_value);
3612         ExecutionContext exe_ctx (GetExecutionContextRef());
3613         valobj_sp = ValueObjectMemory::Create (exe_ctx.GetBestExecutionContextScope(),
3614                                                name,
3615                                                ptr_addr,
3616                                                clang_ast_type);
3617     }
3618     return valobj_sp;
3619 }
3620 
3621 ValueObjectSP
3622 ValueObject::CastPointerType (const char *name, TypeSP &type_sp)
3623 {
3624     ValueObjectSP valobj_sp;
3625     AddressType address_type;
3626     addr_t ptr_value = GetPointerValue (&address_type);
3627 
3628     if (ptr_value != LLDB_INVALID_ADDRESS)
3629     {
3630         Address ptr_addr (ptr_value);
3631         ExecutionContext exe_ctx (GetExecutionContextRef());
3632         valobj_sp = ValueObjectMemory::Create (exe_ctx.GetBestExecutionContextScope(),
3633                                                name,
3634                                                ptr_addr,
3635                                                type_sp);
3636     }
3637     return valobj_sp;
3638 }
3639 
3640 ValueObject::EvaluationPoint::EvaluationPoint () :
3641     m_mod_id(),
3642     m_exe_ctx_ref(),
3643     m_needs_update (true),
3644     m_first_update (true)
3645 {
3646 }
3647 
3648 ValueObject::EvaluationPoint::EvaluationPoint (ExecutionContextScope *exe_scope, bool use_selected):
3649     m_mod_id(),
3650     m_exe_ctx_ref(),
3651     m_needs_update (true),
3652     m_first_update (true)
3653 {
3654     ExecutionContext exe_ctx(exe_scope);
3655     TargetSP target_sp (exe_ctx.GetTargetSP());
3656     if (target_sp)
3657     {
3658         m_exe_ctx_ref.SetTargetSP (target_sp);
3659         ProcessSP process_sp (exe_ctx.GetProcessSP());
3660         if (!process_sp)
3661             process_sp = target_sp->GetProcessSP();
3662 
3663         if (process_sp)
3664         {
3665             m_mod_id = process_sp->GetModID();
3666             m_exe_ctx_ref.SetProcessSP (process_sp);
3667 
3668             ThreadSP thread_sp (exe_ctx.GetThreadSP());
3669 
3670             if (!thread_sp)
3671             {
3672                 if (use_selected)
3673                     thread_sp = process_sp->GetThreadList().GetSelectedThread();
3674             }
3675 
3676             if (thread_sp)
3677             {
3678                 m_exe_ctx_ref.SetThreadSP(thread_sp);
3679 
3680                 StackFrameSP frame_sp (exe_ctx.GetFrameSP());
3681                 if (!frame_sp)
3682                 {
3683                     if (use_selected)
3684                         frame_sp = thread_sp->GetSelectedFrame();
3685                 }
3686                 if (frame_sp)
3687                     m_exe_ctx_ref.SetFrameSP(frame_sp);
3688             }
3689         }
3690     }
3691 }
3692 
3693 ValueObject::EvaluationPoint::EvaluationPoint (const ValueObject::EvaluationPoint &rhs) :
3694     m_mod_id(),
3695     m_exe_ctx_ref(rhs.m_exe_ctx_ref),
3696     m_needs_update (true),
3697     m_first_update (true)
3698 {
3699 }
3700 
3701 ValueObject::EvaluationPoint::~EvaluationPoint ()
3702 {
3703 }
3704 
3705 // This function checks the EvaluationPoint against the current process state.  If the current
3706 // state matches the evaluation point, or the evaluation point is already invalid, then we return
3707 // false, meaning "no change".  If the current state is different, we update our state, and return
3708 // true meaning "yes, change".  If we did see a change, we also set m_needs_update to true, so
3709 // future calls to NeedsUpdate will return true.
3710 // exe_scope will be set to the current execution context scope.
3711 
3712 bool
3713 ValueObject::EvaluationPoint::SyncWithProcessState()
3714 {
3715 
3716     // Start with the target, if it is NULL, then we're obviously not going to get any further:
3717     const bool thread_and_frame_only_if_stopped = true;
3718     ExecutionContext exe_ctx(m_exe_ctx_ref.Lock(thread_and_frame_only_if_stopped));
3719 
3720     if (exe_ctx.GetTargetPtr() == NULL)
3721         return false;
3722 
3723     // If we don't have a process nothing can change.
3724     Process *process = exe_ctx.GetProcessPtr();
3725     if (process == NULL)
3726         return false;
3727 
3728     // If our stop id is the current stop ID, nothing has changed:
3729     ProcessModID current_mod_id = process->GetModID();
3730 
3731     // If the current stop id is 0, either we haven't run yet, or the process state has been cleared.
3732     // In either case, we aren't going to be able to sync with the process state.
3733     if (current_mod_id.GetStopID() == 0)
3734         return false;
3735 
3736     bool changed = false;
3737     const bool was_valid = m_mod_id.IsValid();
3738     if (was_valid)
3739     {
3740         if (m_mod_id == current_mod_id)
3741         {
3742             // Everything is already up to date in this object, no need to
3743             // update the execution context scope.
3744             changed = false;
3745         }
3746         else
3747         {
3748             m_mod_id = current_mod_id;
3749             m_needs_update = true;
3750             changed = true;
3751         }
3752     }
3753 
3754     // Now re-look up the thread and frame in case the underlying objects have gone away & been recreated.
3755     // That way we'll be sure to return a valid exe_scope.
3756     // If we used to have a thread or a frame but can't find it anymore, then mark ourselves as invalid.
3757 
3758     if (m_exe_ctx_ref.HasThreadRef())
3759     {
3760         ThreadSP thread_sp (m_exe_ctx_ref.GetThreadSP());
3761         if (thread_sp)
3762         {
3763             if (m_exe_ctx_ref.HasFrameRef())
3764             {
3765                 StackFrameSP frame_sp (m_exe_ctx_ref.GetFrameSP());
3766                 if (!frame_sp)
3767                 {
3768                     // We used to have a frame, but now it is gone
3769                     SetInvalid();
3770                     changed = was_valid;
3771                 }
3772             }
3773         }
3774         else
3775         {
3776             // We used to have a thread, but now it is gone
3777             SetInvalid();
3778             changed = was_valid;
3779         }
3780 
3781     }
3782     return changed;
3783 }
3784 
3785 void
3786 ValueObject::EvaluationPoint::SetUpdated ()
3787 {
3788     ProcessSP process_sp(m_exe_ctx_ref.GetProcessSP());
3789     if (process_sp)
3790         m_mod_id = process_sp->GetModID();
3791     m_first_update = false;
3792     m_needs_update = false;
3793 }
3794 
3795 
3796 
3797 void
3798 ValueObject::ClearUserVisibleData(uint32_t clear_mask)
3799 {
3800     if ((clear_mask & eClearUserVisibleDataItemsValue) == eClearUserVisibleDataItemsValue)
3801         m_value_str.clear();
3802 
3803     if ((clear_mask & eClearUserVisibleDataItemsLocation) == eClearUserVisibleDataItemsLocation)
3804         m_location_str.clear();
3805 
3806     if ((clear_mask & eClearUserVisibleDataItemsSummary) == eClearUserVisibleDataItemsSummary)
3807     {
3808         m_summary_str.clear();
3809     }
3810 
3811     if ((clear_mask & eClearUserVisibleDataItemsDescription) == eClearUserVisibleDataItemsDescription)
3812         m_object_desc_str.clear();
3813 
3814     if ((clear_mask & eClearUserVisibleDataItemsSyntheticChildren) == eClearUserVisibleDataItemsSyntheticChildren)
3815     {
3816             if (m_synthetic_value)
3817                 m_synthetic_value = NULL;
3818     }
3819 }
3820 
3821 SymbolContextScope *
3822 ValueObject::GetSymbolContextScope()
3823 {
3824     if (m_parent)
3825     {
3826         if (!m_parent->IsPointerOrReferenceType())
3827             return m_parent->GetSymbolContextScope();
3828     }
3829     return NULL;
3830 }
3831 
3832 lldb::ValueObjectSP
3833 ValueObject::CreateValueObjectFromExpression (const char* name,
3834                                               const char* expression,
3835                                               const ExecutionContext& exe_ctx)
3836 {
3837     lldb::ValueObjectSP retval_sp;
3838     lldb::TargetSP target_sp(exe_ctx.GetTargetSP());
3839     if (!target_sp)
3840         return retval_sp;
3841     if (!expression || !*expression)
3842         return retval_sp;
3843     target_sp->EvaluateExpression (expression,
3844                                    exe_ctx.GetFrameSP().get(),
3845                                    retval_sp);
3846     if (retval_sp && name && *name)
3847         retval_sp->SetName(ConstString(name));
3848     return retval_sp;
3849 }
3850 
3851 lldb::ValueObjectSP
3852 ValueObject::CreateValueObjectFromAddress (const char* name,
3853                                            uint64_t address,
3854                                            const ExecutionContext& exe_ctx,
3855                                            ClangASTType type)
3856 {
3857     if (type)
3858     {
3859         ClangASTType pointer_type(type.GetPointerType());
3860         if (pointer_type)
3861         {
3862             lldb::DataBufferSP buffer(new lldb_private::DataBufferHeap(&address,sizeof(lldb::addr_t)));
3863             lldb::ValueObjectSP ptr_result_valobj_sp(ValueObjectConstResult::Create (exe_ctx.GetBestExecutionContextScope(),
3864                                                                                      pointer_type,
3865                                                                                      ConstString(name),
3866                                                                                      buffer,
3867                                                                                      lldb::endian::InlHostByteOrder(),
3868                                                                                      exe_ctx.GetAddressByteSize()));
3869             if (ptr_result_valobj_sp)
3870             {
3871                 ptr_result_valobj_sp->GetValue().SetValueType(Value::eValueTypeLoadAddress);
3872                 Error err;
3873                 ptr_result_valobj_sp = ptr_result_valobj_sp->Dereference(err);
3874                 if (ptr_result_valobj_sp && name && *name)
3875                     ptr_result_valobj_sp->SetName(ConstString(name));
3876             }
3877             return ptr_result_valobj_sp;
3878         }
3879     }
3880     return lldb::ValueObjectSP();
3881 }
3882 
3883 lldb::ValueObjectSP
3884 ValueObject::CreateValueObjectFromData (const char* name,
3885                                         DataExtractor& data,
3886                                         const ExecutionContext& exe_ctx,
3887                                         ClangASTType type)
3888 {
3889     lldb::ValueObjectSP new_value_sp;
3890     new_value_sp = ValueObjectConstResult::Create (exe_ctx.GetBestExecutionContextScope(),
3891                                                    type,
3892                                                    ConstString(name),
3893                                                    data,
3894                                                    LLDB_INVALID_ADDRESS);
3895     new_value_sp->SetAddressTypeOfChildren(eAddressTypeLoad);
3896     if (new_value_sp && name && *name)
3897         new_value_sp->SetName(ConstString(name));
3898     return new_value_sp;
3899 }
3900 
3901 ModuleSP
3902 ValueObject::GetModule ()
3903 {
3904     ValueObject* root(GetRoot());
3905     if (root != this)
3906         return root->GetModule();
3907     return lldb::ModuleSP();
3908 }
3909 
3910 ValueObject*
3911 ValueObject::GetRoot ()
3912 {
3913     if (m_root)
3914         return m_root;
3915     ValueObject* parent = m_parent;
3916     if (!parent)
3917         return (m_root = this);
3918     while (parent->m_parent)
3919     {
3920         if (parent->m_root)
3921             return (m_root = parent->m_root);
3922         parent = parent->m_parent;
3923     }
3924     return (m_root = parent);
3925 }
3926 
3927 AddressType
3928 ValueObject::GetAddressTypeOfChildren()
3929 {
3930     if (m_address_type_of_ptr_or_ref_children == eAddressTypeInvalid)
3931     {
3932         ValueObject* root(GetRoot());
3933         if (root != this)
3934             return root->GetAddressTypeOfChildren();
3935     }
3936     return m_address_type_of_ptr_or_ref_children;
3937 }
3938 
3939 lldb::DynamicValueType
3940 ValueObject::GetDynamicValueType ()
3941 {
3942     ValueObject* with_dv_info = this;
3943     while (with_dv_info)
3944     {
3945         if (with_dv_info->HasDynamicValueTypeInfo())
3946             return with_dv_info->GetDynamicValueTypeImpl();
3947         with_dv_info = with_dv_info->m_parent;
3948     }
3949     return lldb::eNoDynamicValues;
3950 }
3951 
3952 lldb::Format
3953 ValueObject::GetFormat () const
3954 {
3955     const ValueObject* with_fmt_info = this;
3956     while (with_fmt_info)
3957     {
3958         if (with_fmt_info->m_format != lldb::eFormatDefault)
3959             return with_fmt_info->m_format;
3960         with_fmt_info = with_fmt_info->m_parent;
3961     }
3962     return m_format;
3963 }
3964