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