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