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