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