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/Debugger.h"
23 #include "lldb/Core/StreamString.h"
24 #include "lldb/Core/ValueObjectChild.h"
25 #include "lldb/Core/ValueObjectConstResult.h"
26 #include "lldb/Core/ValueObjectDynamicValue.h"
27 #include "lldb/Core/ValueObjectList.h"
28 #include "lldb/Core/ValueObjectMemory.h"
29 
30 #include "lldb/Host/Endian.h"
31 
32 #include "lldb/Symbol/ClangASTType.h"
33 #include "lldb/Symbol/ClangASTContext.h"
34 #include "lldb/Symbol/Type.h"
35 
36 #include "lldb/Target/ExecutionContext.h"
37 #include "lldb/Target/LanguageRuntime.h"
38 #include "lldb/Target/Process.h"
39 #include "lldb/Target/RegisterContext.h"
40 #include "lldb/Target/Target.h"
41 #include "lldb/Target/Thread.h"
42 
43 using namespace lldb;
44 using namespace lldb_private;
45 
46 static lldb::user_id_t g_value_obj_uid = 0;
47 
48 //----------------------------------------------------------------------
49 // ValueObject constructor
50 //----------------------------------------------------------------------
51 ValueObject::ValueObject (ValueObject &parent) :
52     UserID (++g_value_obj_uid), // Unique identifier for every value object
53     m_parent (&parent),
54     m_update_point (parent.GetUpdatePoint ()),
55     m_name (),
56     m_data (),
57     m_value (),
58     m_error (),
59     m_value_str (),
60     m_old_value_str (),
61     m_location_str (),
62     m_summary_str (),
63     m_object_desc_str (),
64     m_manager(parent.GetManager()),
65     m_children (),
66     m_synthetic_children (),
67     m_dynamic_value (NULL),
68     m_deref_valobj(NULL),
69     m_format (eFormatDefault),
70     m_value_is_valid (false),
71     m_value_did_change (false),
72     m_children_count_valid (false),
73     m_old_value_valid (false),
74     m_pointers_point_to_load_addrs (false),
75     m_is_deref_of_parent (false),
76     m_is_array_item_for_pointer(false),
77     m_is_bitfield_for_scalar(false),
78     m_last_format_mgr_revision(0),
79     m_last_summary_format(),
80     m_last_value_format(),
81     m_forced_summary_format()
82 {
83     m_manager->ManageObject(this);
84 }
85 
86 //----------------------------------------------------------------------
87 // ValueObject constructor
88 //----------------------------------------------------------------------
89 ValueObject::ValueObject (ExecutionContextScope *exe_scope) :
90     UserID (++g_value_obj_uid), // Unique identifier for every value object
91     m_parent (NULL),
92     m_update_point (exe_scope),
93     m_name (),
94     m_data (),
95     m_value (),
96     m_error (),
97     m_value_str (),
98     m_old_value_str (),
99     m_location_str (),
100     m_summary_str (),
101     m_object_desc_str (),
102     m_manager(),
103     m_children (),
104     m_synthetic_children (),
105     m_dynamic_value (NULL),
106     m_deref_valobj(NULL),
107     m_format (eFormatDefault),
108     m_value_is_valid (false),
109     m_value_did_change (false),
110     m_children_count_valid (false),
111     m_old_value_valid (false),
112     m_pointers_point_to_load_addrs (false),
113     m_is_deref_of_parent (false),
114     m_is_array_item_for_pointer(false),
115     m_is_bitfield_for_scalar(false),
116     m_last_format_mgr_revision(0),
117     m_last_summary_format(),
118     m_last_value_format(),
119     m_forced_summary_format()
120 {
121     m_manager = new ValueObjectManager();
122     m_manager->ManageObject (this);
123 }
124 
125 //----------------------------------------------------------------------
126 // Destructor
127 //----------------------------------------------------------------------
128 ValueObject::~ValueObject ()
129 {
130 }
131 
132 bool
133 ValueObject::UpdateValueIfNeeded (bool update_format)
134 {
135 
136     if (update_format)
137         UpdateFormatsIfNeeded();
138 
139     // If this is a constant value, then our success is predicated on whether
140     // we have an error or not
141     if (GetIsConstant())
142         return m_error.Success();
143 
144     bool first_update = m_update_point.IsFirstEvaluation();
145 
146     if (m_update_point.NeedsUpdating())
147     {
148         m_update_point.SetUpdated();
149 
150         // Save the old value using swap to avoid a string copy which
151         // also will clear our m_value_str
152         if (m_value_str.empty())
153         {
154             m_old_value_valid = false;
155         }
156         else
157         {
158             m_old_value_valid = true;
159             m_old_value_str.swap (m_value_str);
160             m_value_str.clear();
161         }
162         m_location_str.clear();
163         m_summary_str.clear();
164         m_object_desc_str.clear();
165 
166         const bool value_was_valid = GetValueIsValid();
167         SetValueDidChange (false);
168 
169         m_error.Clear();
170 
171         // Call the pure virtual function to update the value
172         bool success = UpdateValue ();
173 
174         SetValueIsValid (success);
175 
176         if (first_update)
177             SetValueDidChange (false);
178         else if (!m_value_did_change && success == false)
179         {
180             // The value wasn't gotten successfully, so we mark this
181             // as changed if the value used to be valid and now isn't
182             SetValueDidChange (value_was_valid);
183         }
184     }
185     return m_error.Success();
186 }
187 
188 void
189 ValueObject::UpdateFormatsIfNeeded()
190 {
191     /*printf("CHECKING FOR UPDATES. I am at revision %d, while the format manager is at revision %d\n",
192            m_last_format_mgr_revision,
193            Debugger::ValueFormats::GetCurrentRevision());*/
194     if (HasCustomSummaryFormat() && m_update_point.GetUpdateID() != m_user_id_of_forced_summary)
195     {
196         ClearCustomSummaryFormat();
197         m_summary_str.clear();
198     }
199     if (m_last_format_mgr_revision != Debugger::ValueFormats::GetCurrentRevision())
200     {
201         if (m_last_summary_format.get())
202             m_last_summary_format.reset((SummaryFormat*)NULL);
203         if (m_last_value_format.get())
204             m_last_value_format.reset((ValueFormat*)NULL);
205         Debugger::ValueFormats::Get(*this, m_last_value_format);
206         if (!Debugger::SummaryFormats::Get(*this, m_last_summary_format))
207             Debugger::RegexSummaryFormats::Get(*this, m_last_summary_format);
208         m_last_format_mgr_revision = Debugger::ValueFormats::GetCurrentRevision();
209         m_value_str.clear();
210         m_summary_str.clear();
211     }
212 }
213 
214 DataExtractor &
215 ValueObject::GetDataExtractor ()
216 {
217     UpdateValueIfNeeded();
218     return m_data;
219 }
220 
221 const Error &
222 ValueObject::GetError()
223 {
224     UpdateValueIfNeeded();
225     return m_error;
226 }
227 
228 const ConstString &
229 ValueObject::GetName() const
230 {
231     return m_name;
232 }
233 
234 const char *
235 ValueObject::GetLocationAsCString ()
236 {
237     if (UpdateValueIfNeeded())
238     {
239         if (m_location_str.empty())
240         {
241             StreamString sstr;
242 
243             switch (m_value.GetValueType())
244             {
245             default:
246                 break;
247 
248             case Value::eValueTypeScalar:
249                 if (m_value.GetContextType() == Value::eContextTypeRegisterInfo)
250                 {
251                     RegisterInfo *reg_info = m_value.GetRegisterInfo();
252                     if (reg_info)
253                     {
254                         if (reg_info->name)
255                             m_location_str = reg_info->name;
256                         else if (reg_info->alt_name)
257                             m_location_str = reg_info->alt_name;
258                         break;
259                     }
260                 }
261                 m_location_str = "scalar";
262                 break;
263 
264             case Value::eValueTypeLoadAddress:
265             case Value::eValueTypeFileAddress:
266             case Value::eValueTypeHostAddress:
267                 {
268                     uint32_t addr_nibble_size = m_data.GetAddressByteSize() * 2;
269                     sstr.Printf("0x%*.*llx", addr_nibble_size, addr_nibble_size, m_value.GetScalar().ULongLong(LLDB_INVALID_ADDRESS));
270                     m_location_str.swap(sstr.GetString());
271                 }
272                 break;
273             }
274         }
275     }
276     return m_location_str.c_str();
277 }
278 
279 Value &
280 ValueObject::GetValue()
281 {
282     return m_value;
283 }
284 
285 const Value &
286 ValueObject::GetValue() const
287 {
288     return m_value;
289 }
290 
291 bool
292 ValueObject::ResolveValue (Scalar &scalar)
293 {
294     ExecutionContext exe_ctx;
295     ExecutionContextScope *exe_scope = GetExecutionContextScope();
296     if (exe_scope)
297         exe_scope->CalculateExecutionContext(exe_ctx);
298     scalar = m_value.ResolveValue(&exe_ctx, GetClangAST ());
299     return scalar.IsValid();
300 }
301 
302 bool
303 ValueObject::GetValueIsValid () const
304 {
305     return m_value_is_valid;
306 }
307 
308 
309 void
310 ValueObject::SetValueIsValid (bool b)
311 {
312     m_value_is_valid = b;
313 }
314 
315 bool
316 ValueObject::GetValueDidChange ()
317 {
318     GetValueAsCString ();
319     return m_value_did_change;
320 }
321 
322 void
323 ValueObject::SetValueDidChange (bool value_changed)
324 {
325     m_value_did_change = value_changed;
326 }
327 
328 ValueObjectSP
329 ValueObject::GetChildAtIndex (uint32_t idx, bool can_create)
330 {
331     ValueObjectSP child_sp;
332     // We may need to update our value if we are dynamic
333     if (IsPossibleDynamicType ())
334         UpdateValueIfNeeded();
335     if (idx < GetNumChildren())
336     {
337         // Check if we have already made the child value object?
338         if (can_create && m_children[idx] == NULL)
339         {
340             // No we haven't created the child at this index, so lets have our
341             // subclass do it and cache the result for quick future access.
342             m_children[idx] = CreateChildAtIndex (idx, false, 0);
343         }
344 
345         if (m_children[idx] != NULL)
346             return m_children[idx]->GetSP();
347     }
348     return child_sp;
349 }
350 
351 uint32_t
352 ValueObject::GetIndexOfChildWithName (const ConstString &name)
353 {
354     bool omit_empty_base_classes = true;
355     return ClangASTContext::GetIndexOfChildWithName (GetClangAST(),
356                                                      GetClangType(),
357                                                      name.GetCString(),
358                                                      omit_empty_base_classes);
359 }
360 
361 ValueObjectSP
362 ValueObject::GetChildMemberWithName (const ConstString &name, bool can_create)
363 {
364     // when getting a child by name, it could be buried inside some base
365     // classes (which really aren't part of the expression path), so we
366     // need a vector of indexes that can get us down to the correct child
367     ValueObjectSP child_sp;
368 
369     // We may need to update our value if we are dynamic
370     if (IsPossibleDynamicType ())
371         UpdateValueIfNeeded();
372 
373     std::vector<uint32_t> child_indexes;
374     clang::ASTContext *clang_ast = GetClangAST();
375     void *clang_type = GetClangType();
376     bool omit_empty_base_classes = true;
377     const size_t num_child_indexes =  ClangASTContext::GetIndexOfChildMemberWithName (clang_ast,
378                                                                                       clang_type,
379                                                                                       name.GetCString(),
380                                                                                       omit_empty_base_classes,
381                                                                                       child_indexes);
382     if (num_child_indexes > 0)
383     {
384         std::vector<uint32_t>::const_iterator pos = child_indexes.begin ();
385         std::vector<uint32_t>::const_iterator end = child_indexes.end ();
386 
387         child_sp = GetChildAtIndex(*pos, can_create);
388         for (++pos; pos != end; ++pos)
389         {
390             if (child_sp)
391             {
392                 ValueObjectSP new_child_sp(child_sp->GetChildAtIndex (*pos, can_create));
393                 child_sp = new_child_sp;
394             }
395             else
396             {
397                 child_sp.reset();
398             }
399 
400         }
401     }
402     return child_sp;
403 }
404 
405 
406 uint32_t
407 ValueObject::GetNumChildren ()
408 {
409     if (!m_children_count_valid)
410     {
411         SetNumChildren (CalculateNumChildren());
412     }
413     return m_children.size();
414 }
415 void
416 ValueObject::SetNumChildren (uint32_t num_children)
417 {
418     m_children_count_valid = true;
419     m_children.resize(num_children);
420 }
421 
422 void
423 ValueObject::SetName (const char *name)
424 {
425     m_name.SetCString(name);
426 }
427 
428 void
429 ValueObject::SetName (const ConstString &name)
430 {
431     m_name = name;
432 }
433 
434 ValueObject *
435 ValueObject::CreateChildAtIndex (uint32_t idx, bool synthetic_array_member, int32_t synthetic_index)
436 {
437     ValueObject *valobj = NULL;
438 
439     bool omit_empty_base_classes = true;
440     bool ignore_array_bounds = synthetic_array_member;
441     std::string child_name_str;
442     uint32_t child_byte_size = 0;
443     int32_t child_byte_offset = 0;
444     uint32_t child_bitfield_bit_size = 0;
445     uint32_t child_bitfield_bit_offset = 0;
446     bool child_is_base_class = false;
447     bool child_is_deref_of_parent = false;
448 
449     const bool transparent_pointers = synthetic_array_member == false;
450     clang::ASTContext *clang_ast = GetClangAST();
451     clang_type_t clang_type = GetClangType();
452     clang_type_t child_clang_type;
453 
454     ExecutionContext exe_ctx;
455     GetExecutionContextScope()->CalculateExecutionContext (exe_ctx);
456 
457     child_clang_type = ClangASTContext::GetChildClangTypeAtIndex (&exe_ctx,
458                                                                   clang_ast,
459                                                                   GetName().GetCString(),
460                                                                   clang_type,
461                                                                   idx,
462                                                                   transparent_pointers,
463                                                                   omit_empty_base_classes,
464                                                                   ignore_array_bounds,
465                                                                   child_name_str,
466                                                                   child_byte_size,
467                                                                   child_byte_offset,
468                                                                   child_bitfield_bit_size,
469                                                                   child_bitfield_bit_offset,
470                                                                   child_is_base_class,
471                                                                   child_is_deref_of_parent);
472     if (child_clang_type && child_byte_size)
473     {
474         if (synthetic_index)
475             child_byte_offset += child_byte_size * synthetic_index;
476 
477         ConstString child_name;
478         if (!child_name_str.empty())
479             child_name.SetCString (child_name_str.c_str());
480 
481         valobj = new ValueObjectChild (*this,
482                                        clang_ast,
483                                        child_clang_type,
484                                        child_name,
485                                        child_byte_size,
486                                        child_byte_offset,
487                                        child_bitfield_bit_size,
488                                        child_bitfield_bit_offset,
489                                        child_is_base_class,
490                                        child_is_deref_of_parent);
491         if (m_pointers_point_to_load_addrs)
492             valobj->SetPointersPointToLoadAddrs (m_pointers_point_to_load_addrs);
493     }
494 
495     return valobj;
496 }
497 
498 const char *
499 ValueObject::GetSummaryAsCString ()
500 {
501     if (UpdateValueIfNeeded ())
502     {
503         if (m_summary_str.empty())
504         {
505             SummaryFormat* summary_format = GetSummaryFormat().get();
506 
507             if (summary_format)
508             {
509                 StreamString s;
510                 ExecutionContext exe_ctx;
511                 this->GetExecutionContextScope()->CalculateExecutionContext(exe_ctx);
512                 SymbolContext sc;
513                 if (exe_ctx.frame)
514                     sc = exe_ctx.frame->GetSymbolContext(eSymbolContextEverything);
515 
516                 if (summary_format->m_show_members_oneliner)
517                 {
518                     const uint32_t num_children = GetNumChildren();
519                     if (num_children)
520                     {
521 
522                         s.PutChar('(');
523 
524                         for (uint32_t idx=0; idx<num_children; ++idx)
525                         {
526                             ValueObjectSP child_sp(GetChildAtIndex(idx, true));
527                             if (child_sp.get())
528                             {
529                                 if (idx)
530                                     s.PutCString(", ");
531                                 s.PutCString(child_sp.get()->GetName().AsCString());
532                                 s.PutChar('=');
533                                 s.PutCString(child_sp.get()->GetPrintableRepresentation());
534                             }
535                         }
536 
537                         s.PutChar(')');
538 
539                         m_summary_str.swap(s.GetString());
540                         return m_summary_str.c_str();
541                     }
542                     else
543                         return "()";
544 
545                 }
546                 else
547                 {
548                     if (Debugger::FormatPrompt(summary_format->m_format.c_str(), &sc, &exe_ctx, &sc.line_entry.range.GetBaseAddress(), s, NULL, this))
549                     {
550                         m_summary_str.swap(s.GetString());
551                         return m_summary_str.c_str();
552                     }
553                     else
554                         return NULL;
555                 }
556             }
557 
558             clang_type_t clang_type = GetClangType();
559 
560             // See if this is a pointer to a C string?
561             if (clang_type)
562             {
563                 StreamString sstr;
564                 clang_type_t elem_or_pointee_clang_type;
565                 const Flags type_flags (ClangASTContext::GetTypeInfo (clang_type,
566                                                                       GetClangAST(),
567                                                                       &elem_or_pointee_clang_type));
568 
569                 ExecutionContextScope *exe_scope = GetExecutionContextScope();
570                 if (exe_scope)
571                 {
572                     if (type_flags.AnySet (ClangASTContext::eTypeIsArray | ClangASTContext::eTypeIsPointer) &&
573                         ClangASTContext::IsCharType (elem_or_pointee_clang_type))
574                     {
575                         Target *target = exe_scope->CalculateTarget();
576                         if (target != NULL)
577                         {
578                             lldb::addr_t cstr_address = LLDB_INVALID_ADDRESS;
579                             AddressType cstr_address_type = eAddressTypeInvalid;
580 
581                             size_t cstr_len = 0;
582                             bool capped_data = false;
583                             if (type_flags.Test (ClangASTContext::eTypeIsArray))
584                             {
585                                 // We have an array
586                                 cstr_len = ClangASTContext::GetArraySize (clang_type);
587                                 if (cstr_len > 512) // TODO: make cap a setting
588                                 {
589                                     cstr_len = ClangASTContext::GetArraySize (clang_type);
590                                     if (cstr_len > 512) // TODO: make cap a setting
591                                     {
592                                         capped_data = true;
593                                         cstr_len = 512;
594                                     }
595                                 }
596                                 cstr_address = GetAddressOf (cstr_address_type, true);
597                             }
598                             else
599                             {
600                                 // We have a pointer
601                                 cstr_address = GetPointerValue (cstr_address_type, true);
602                             }
603                             if (cstr_address != LLDB_INVALID_ADDRESS)
604                             {
605                                 Address cstr_so_addr (NULL, cstr_address);
606                                 DataExtractor data;
607                                 size_t bytes_read = 0;
608                                 std::vector<char> data_buffer;
609                                 Error error;
610                                 bool prefer_file_cache = false;
611                                 if (cstr_len > 0)
612                                 {
613                                     data_buffer.resize(cstr_len);
614                                     data.SetData (&data_buffer.front(), data_buffer.size(), lldb::endian::InlHostByteOrder());
615                                     bytes_read = target->ReadMemory (cstr_so_addr,
616                                                                      prefer_file_cache,
617                                                                      &data_buffer.front(),
618                                                                      cstr_len,
619                                                                      error);
620                                     if (bytes_read > 0)
621                                     {
622                                         sstr << '"';
623                                         data.Dump (&sstr,
624                                                    0,                 // Start offset in "data"
625                                                    eFormatCharArray,  // Print as characters
626                                                    1,                 // Size of item (1 byte for a char!)
627                                                    bytes_read,        // How many bytes to print?
628                                                    UINT32_MAX,        // num per line
629                                                    LLDB_INVALID_ADDRESS,// base address
630                                                    0,                 // bitfield bit size
631                                                    0);                // bitfield bit offset
632                                         if (capped_data)
633                                             sstr << "...";
634                                         sstr << '"';
635                                     }
636                                 }
637                                 else
638                                 {
639                                     const size_t k_max_buf_size = 256;
640                                     data_buffer.resize (k_max_buf_size + 1);
641                                     // NULL terminate in case we don't get the entire C string
642                                     data_buffer.back() = '\0';
643 
644                                     sstr << '"';
645 
646                                     data.SetData (&data_buffer.front(), data_buffer.size(), endian::InlHostByteOrder());
647                                     while ((bytes_read = target->ReadMemory (cstr_so_addr,
648                                                                              prefer_file_cache,
649                                                                              &data_buffer.front(),
650                                                                              k_max_buf_size,
651                                                                              error)) > 0)
652                                     {
653                                         size_t len = strlen(&data_buffer.front());
654                                         if (len == 0)
655                                             break;
656                                         if (len > bytes_read)
657                                             len = bytes_read;
658 
659                                         data.Dump (&sstr,
660                                                    0,                 // Start offset in "data"
661                                                    eFormatCharArray,  // Print as characters
662                                                    1,                 // Size of item (1 byte for a char!)
663                                                    len,               // How many bytes to print?
664                                                    UINT32_MAX,        // num per line
665                                                    LLDB_INVALID_ADDRESS,// base address
666                                                    0,                 // bitfield bit size
667                                                    0);                // bitfield bit offset
668 
669                                         if (len < k_max_buf_size)
670                                             break;
671                                         cstr_so_addr.Slide (k_max_buf_size);
672                                     }
673                                     sstr << '"';
674                                 }
675                             }
676                         }
677 
678                         if (sstr.GetSize() > 0)
679                             m_summary_str.assign (sstr.GetData(), sstr.GetSize());
680                     }
681                     else if (ClangASTContext::IsFunctionPointerType (clang_type))
682                     {
683                         AddressType func_ptr_address_type = eAddressTypeInvalid;
684                         lldb::addr_t func_ptr_address = GetPointerValue (func_ptr_address_type, true);
685 
686                         if (func_ptr_address != 0 && func_ptr_address != LLDB_INVALID_ADDRESS)
687                         {
688                             switch (func_ptr_address_type)
689                             {
690                             case eAddressTypeInvalid:
691                             case eAddressTypeFile:
692                                 break;
693 
694                             case eAddressTypeLoad:
695                                 {
696                                     Address so_addr;
697                                     Target *target = exe_scope->CalculateTarget();
698                                     if (target && target->GetSectionLoadList().IsEmpty() == false)
699                                     {
700                                         if (target->GetSectionLoadList().ResolveLoadAddress(func_ptr_address, so_addr))
701                                         {
702                                             so_addr.Dump (&sstr,
703                                                           exe_scope,
704                                                           Address::DumpStyleResolvedDescription,
705                                                           Address::DumpStyleSectionNameOffset);
706                                         }
707                                     }
708                                 }
709                                 break;
710 
711                             case eAddressTypeHost:
712                                 break;
713                             }
714                         }
715                         if (sstr.GetSize() > 0)
716                         {
717                             m_summary_str.assign (1, '(');
718                             m_summary_str.append (sstr.GetData(), sstr.GetSize());
719                             m_summary_str.append (1, ')');
720                         }
721                     }
722                 }
723             }
724         }
725     }
726     if (m_summary_str.empty())
727         return NULL;
728     return m_summary_str.c_str();
729 }
730 
731 bool
732 ValueObject::IsCStringContainer(bool check_pointer)
733 {
734     clang_type_t elem_or_pointee_clang_type;
735     const Flags type_flags (ClangASTContext::GetTypeInfo (GetClangType(),
736                                                           GetClangAST(),
737                                                           &elem_or_pointee_clang_type));
738     bool is_char_arr_ptr (type_flags.AnySet (ClangASTContext::eTypeIsArray | ClangASTContext::eTypeIsPointer) &&
739             ClangASTContext::IsCharType (elem_or_pointee_clang_type));
740     if (!is_char_arr_ptr)
741         return false;
742     if (!check_pointer)
743         return true;
744     if (type_flags.Test(ClangASTContext::eTypeIsArray))
745         return true;
746     lldb::addr_t cstr_address = LLDB_INVALID_ADDRESS;
747     AddressType cstr_address_type = eAddressTypeInvalid;
748     cstr_address = GetAddressOf (cstr_address_type, true);
749     return (cstr_address != LLDB_INVALID_ADDRESS);
750 }
751 
752 void
753 ValueObject::ReadPointedString(Stream& s,
754                                Error& error,
755                                uint32_t max_length)
756 {
757 
758     if (max_length == 0)
759         max_length = 128;   // this should be a setting, or a formatting parameter
760 
761     clang_type_t clang_type = GetClangType();
762     clang_type_t elem_or_pointee_clang_type;
763     const Flags type_flags (ClangASTContext::GetTypeInfo (clang_type,
764                                                           GetClangAST(),
765                                                           &elem_or_pointee_clang_type));
766     if (type_flags.AnySet (ClangASTContext::eTypeIsArray | ClangASTContext::eTypeIsPointer) &&
767         ClangASTContext::IsCharType (elem_or_pointee_clang_type))
768     {
769         ExecutionContextScope *exe_scope = GetExecutionContextScope();
770             if (exe_scope)
771             {
772                 Target *target = exe_scope->CalculateTarget();
773                 if (target != NULL)
774                 {
775                     lldb::addr_t cstr_address = LLDB_INVALID_ADDRESS;
776                     AddressType cstr_address_type = eAddressTypeInvalid;
777 
778                     size_t cstr_len = 0;
779                     bool capped_data = false;
780                     if (type_flags.Test (ClangASTContext::eTypeIsArray))
781                     {
782                         // We have an array
783                         cstr_len = ClangASTContext::GetArraySize (clang_type);
784                         if (cstr_len > max_length) // TODO: make cap a setting
785                         {
786                             cstr_len = ClangASTContext::GetArraySize (clang_type);
787                             if (cstr_len > max_length) // TODO: make cap a setting
788                             {
789                                 capped_data = true;
790                                 cstr_len = max_length;
791                             }
792                         }
793                         cstr_address = GetAddressOf (cstr_address_type, true);
794                     }
795                     else
796                     {
797                         // We have a pointer
798                         cstr_address = GetPointerValue (cstr_address_type, true);
799                     }
800                     if (cstr_address != LLDB_INVALID_ADDRESS)
801                     {
802                         Address cstr_so_addr (NULL, cstr_address);
803                         DataExtractor data;
804                         size_t bytes_read = 0;
805                         std::vector<char> data_buffer;
806                         Error error;
807                         bool prefer_file_cache = false;
808                         if (cstr_len > 0)
809                         {
810                             data_buffer.resize(cstr_len);
811                             data.SetData (&data_buffer.front(), data_buffer.size(), lldb::endian::InlHostByteOrder());
812                             bytes_read = target->ReadMemory (cstr_so_addr,
813                                                              prefer_file_cache,
814                                                              &data_buffer.front(),
815                                                              cstr_len,
816                                                              error);
817                             if (bytes_read > 0)
818                             {
819                                 s << '"';
820                                 data.Dump (&s,
821                                            0,                 // Start offset in "data"
822                                            eFormatCharArray,  // Print as characters
823                                            1,                 // Size of item (1 byte for a char!)
824                                            bytes_read,        // How many bytes to print?
825                                            UINT32_MAX,        // num per line
826                                            LLDB_INVALID_ADDRESS,// base address
827                                            0,                 // bitfield bit size
828                                            0);                // bitfield bit offset
829                                 if (capped_data)
830                                     s << "...";
831                                 s << '"';
832                             }
833                         }
834                         else
835                         {
836                             const size_t k_max_buf_size = 256;
837                             data_buffer.resize (k_max_buf_size + 1);
838                             // NULL terminate in case we don't get the entire C string
839                             data_buffer.back() = '\0';
840 
841                             s << '"';
842 
843                             data.SetData (&data_buffer.front(), data_buffer.size(), endian::InlHostByteOrder());
844                             while ((bytes_read = target->ReadMemory (cstr_so_addr,
845                                                                      prefer_file_cache,
846                                                                      &data_buffer.front(),
847                                                                      k_max_buf_size,
848                                                                      error)) > 0)
849                             {
850                                 size_t len = strlen(&data_buffer.front());
851                                 if (len == 0)
852                                     break;
853                                 if (len > bytes_read)
854                                     len = bytes_read;
855 
856                                 data.Dump (&s,
857                                            0,                 // Start offset in "data"
858                                            eFormatCharArray,  // Print as characters
859                                            1,                 // Size of item (1 byte for a char!)
860                                            len,               // How many bytes to print?
861                                            UINT32_MAX,        // num per line
862                                            LLDB_INVALID_ADDRESS,// base address
863                                            0,                 // bitfield bit size
864                                            0);                // bitfield bit offset
865 
866                                 if (len < k_max_buf_size)
867                                     break;
868                                 cstr_so_addr.Slide (k_max_buf_size);
869                             }
870                             s << '"';
871                         }
872                     }
873                 }
874             }
875     }
876     else
877     {
878         error.SetErrorString("impossible to read a string from this object");
879     }
880 }
881 
882 const char *
883 ValueObject::GetObjectDescription ()
884 {
885 
886     if (!UpdateValueIfNeeded ())
887         return NULL;
888 
889     if (!m_object_desc_str.empty())
890         return m_object_desc_str.c_str();
891 
892     ExecutionContextScope *exe_scope = GetExecutionContextScope();
893     if (exe_scope == NULL)
894         return NULL;
895 
896     Process *process = exe_scope->CalculateProcess();
897     if (process == NULL)
898         return NULL;
899 
900     StreamString s;
901 
902     lldb::LanguageType language = GetObjectRuntimeLanguage();
903     LanguageRuntime *runtime = process->GetLanguageRuntime(language);
904 
905     if (runtime == NULL)
906     {
907         // Aw, hell, if the things a pointer, or even just an integer, let's try ObjC anyway...
908         clang_type_t opaque_qual_type = GetClangType();
909         if (opaque_qual_type != NULL)
910         {
911             bool is_signed;
912             if (ClangASTContext::IsIntegerType (opaque_qual_type, is_signed)
913                 || ClangASTContext::IsPointerType (opaque_qual_type))
914             {
915                 runtime = process->GetLanguageRuntime(lldb::eLanguageTypeObjC);
916             }
917         }
918     }
919 
920     if (runtime && runtime->GetObjectDescription(s, *this))
921     {
922         m_object_desc_str.append (s.GetData());
923     }
924 
925     if (m_object_desc_str.empty())
926         return NULL;
927     else
928         return m_object_desc_str.c_str();
929 }
930 
931 const char *
932 ValueObject::GetValueAsCString ()
933 {
934     // If our byte size is zero this is an aggregate type that has children
935     if (ClangASTContext::IsAggregateType (GetClangType()) == false)
936     {
937         if (UpdateValueIfNeeded())
938         {
939             if (m_value_str.empty())
940             {
941                 const Value::ContextType context_type = m_value.GetContextType();
942 
943                 switch (context_type)
944                 {
945                 case Value::eContextTypeClangType:
946                 case Value::eContextTypeLLDBType:
947                 case Value::eContextTypeVariable:
948                     {
949                         clang_type_t clang_type = GetClangType ();
950                         if (clang_type)
951                         {
952                             StreamString sstr;
953                             Format format = GetFormat();
954                             if (format == eFormatDefault)
955                             {
956                                 if (m_last_value_format)
957                                     format = m_last_value_format->m_format;
958                                 else
959                                     // force the system into using unsigned integers for bitfields
960                                     format = (m_is_bitfield_for_scalar ? eFormatUnsigned :
961                                     ClangASTType::GetFormat(clang_type));
962                             }
963 
964                             if (ClangASTType::DumpTypeValue (GetClangAST(),            // The clang AST
965                                                              clang_type,               // The clang type to display
966                                                              &sstr,
967                                                              format,                   // Format to display this type with
968                                                              m_data,                   // Data to extract from
969                                                              0,                        // Byte offset into "m_data"
970                                                              GetByteSize(),            // Byte size of item in "m_data"
971                                                              GetBitfieldBitSize(),     // Bitfield bit size
972                                                              GetBitfieldBitOffset()))  // Bitfield bit offset
973                                 m_value_str.swap(sstr.GetString());
974                             else
975                             {
976                                 m_error.SetErrorStringWithFormat ("unsufficient data for value (only %u of %u bytes available)",
977                                                                   m_data.GetByteSize(),
978                                                                   GetByteSize());
979                                 m_value_str.clear();
980                             }
981                         }
982                     }
983                     break;
984 
985                 case Value::eContextTypeRegisterInfo:
986                     {
987                         const RegisterInfo *reg_info = m_value.GetRegisterInfo();
988                         if (reg_info)
989                         {
990                             StreamString reg_sstr;
991                             m_data.Dump(&reg_sstr, 0, reg_info->format, reg_info->byte_size, 1, UINT32_MAX, LLDB_INVALID_ADDRESS, 0, 0);
992                             m_value_str.swap(reg_sstr.GetString());
993                         }
994                     }
995                     break;
996 
997                 default:
998                     break;
999                 }
1000             }
1001 
1002             if (!m_value_did_change && m_old_value_valid)
1003             {
1004                 // The value was gotten successfully, so we consider the
1005                 // value as changed if the value string differs
1006                 SetValueDidChange (m_old_value_str != m_value_str);
1007             }
1008         }
1009     }
1010     if (m_value_str.empty())
1011         return NULL;
1012     return m_value_str.c_str();
1013 }
1014 
1015 const char *
1016 ValueObject::GetPrintableRepresentation(ValueObjectRepresentationStyle val_obj_display,
1017                                         lldb::Format custom_format)
1018 {
1019     if(custom_format != lldb::eFormatInvalid)
1020         SetFormat(custom_format);
1021 
1022     const char * return_value;
1023 
1024     switch(val_obj_display)
1025     {
1026         case eDisplayValue:
1027             return_value = GetValueAsCString();
1028             break;
1029         case eDisplaySummary:
1030             return_value = GetSummaryAsCString();
1031             break;
1032         case eDisplayLanguageSpecific:
1033             return_value = GetObjectDescription();
1034             break;
1035     }
1036 
1037     if (!return_value)
1038     {
1039         // try to pick the other choice
1040         if (val_obj_display == eDisplayValue)
1041             return_value = GetSummaryAsCString();
1042         else if (val_obj_display == eDisplaySummary)
1043             return_value = GetValueAsCString();
1044         else
1045             return_value = "";
1046     }
1047 
1048     return (return_value ? return_value : "");
1049 
1050 }
1051 
1052 bool
1053 ValueObject::DumpPrintableRepresentation(Stream& s,
1054                                          ValueObjectRepresentationStyle val_obj_display,
1055                                          lldb::Format custom_format)
1056 {
1057 
1058     if (IsCStringContainer(true) &&
1059         val_obj_display == ValueObject::eDisplayValue &&
1060         custom_format == lldb::eFormatCString)
1061     {
1062         Error error;
1063         ReadPointedString(s, error);
1064         return error.Success();
1065     }
1066     else
1067     {
1068         const char *targetvalue = GetPrintableRepresentation(val_obj_display, custom_format);
1069         if(targetvalue)
1070             s.PutCString(targetvalue);
1071         bool var_success = (targetvalue != NULL);
1072         if(custom_format != eFormatInvalid)
1073             SetFormat(eFormatDefault);
1074         return var_success;
1075     }
1076 }
1077 
1078 addr_t
1079 ValueObject::GetAddressOf (AddressType &address_type, bool scalar_is_load_address)
1080 {
1081     if (!UpdateValueIfNeeded())
1082         return LLDB_INVALID_ADDRESS;
1083 
1084     switch (m_value.GetValueType())
1085     {
1086     case Value::eValueTypeScalar:
1087         if (scalar_is_load_address)
1088         {
1089             address_type = eAddressTypeLoad;
1090             return m_value.GetScalar().ULongLong(LLDB_INVALID_ADDRESS);
1091         }
1092         break;
1093 
1094     case Value::eValueTypeLoadAddress:
1095     case Value::eValueTypeFileAddress:
1096     case Value::eValueTypeHostAddress:
1097         {
1098             address_type = m_value.GetValueAddressType ();
1099             return m_value.GetScalar().ULongLong(LLDB_INVALID_ADDRESS);
1100         }
1101         break;
1102     }
1103     address_type = eAddressTypeInvalid;
1104     return LLDB_INVALID_ADDRESS;
1105 }
1106 
1107 addr_t
1108 ValueObject::GetPointerValue (AddressType &address_type, bool scalar_is_load_address)
1109 {
1110     lldb::addr_t address = LLDB_INVALID_ADDRESS;
1111     address_type = eAddressTypeInvalid;
1112 
1113     if (!UpdateValueIfNeeded())
1114         return address;
1115 
1116     switch (m_value.GetValueType())
1117     {
1118     case Value::eValueTypeScalar:
1119         if (scalar_is_load_address)
1120         {
1121             address = m_value.GetScalar().ULongLong(LLDB_INVALID_ADDRESS);
1122             address_type = eAddressTypeLoad;
1123         }
1124         break;
1125 
1126     case Value::eValueTypeLoadAddress:
1127     case Value::eValueTypeFileAddress:
1128     case Value::eValueTypeHostAddress:
1129         {
1130             uint32_t data_offset = 0;
1131             address = m_data.GetPointer(&data_offset);
1132             address_type = m_value.GetValueAddressType();
1133             if (address_type == eAddressTypeInvalid)
1134                 address_type = eAddressTypeLoad;
1135         }
1136         break;
1137     }
1138 
1139     if (m_pointers_point_to_load_addrs)
1140         address_type = eAddressTypeLoad;
1141 
1142     return address;
1143 }
1144 
1145 bool
1146 ValueObject::SetValueFromCString (const char *value_str)
1147 {
1148     // Make sure our value is up to date first so that our location and location
1149     // type is valid.
1150     if (!UpdateValueIfNeeded())
1151         return false;
1152 
1153     uint32_t count = 0;
1154     lldb::Encoding encoding = ClangASTType::GetEncoding (GetClangType(), count);
1155 
1156     char *end = NULL;
1157     const size_t byte_size = GetByteSize();
1158     switch (encoding)
1159     {
1160     case eEncodingInvalid:
1161         return false;
1162 
1163     case eEncodingUint:
1164         if (byte_size > sizeof(unsigned long long))
1165         {
1166             return false;
1167         }
1168         else
1169         {
1170             unsigned long long ull_val = strtoull(value_str, &end, 0);
1171             if (end && *end != '\0')
1172                 return false;
1173             m_value.GetScalar() = ull_val;
1174             // Limit the bytes in our m_data appropriately.
1175             m_value.GetScalar().GetData (m_data, byte_size);
1176         }
1177         break;
1178 
1179     case eEncodingSint:
1180         if (byte_size > sizeof(long long))
1181         {
1182             return false;
1183         }
1184         else
1185         {
1186             long long sll_val = strtoll(value_str, &end, 0);
1187             if (end && *end != '\0')
1188                 return false;
1189             m_value.GetScalar() = sll_val;
1190             // Limit the bytes in our m_data appropriately.
1191             m_value.GetScalar().GetData (m_data, byte_size);
1192         }
1193         break;
1194 
1195     case eEncodingIEEE754:
1196         {
1197             const off_t byte_offset = GetByteOffset();
1198             uint8_t *dst = const_cast<uint8_t *>(m_data.PeekData(byte_offset, byte_size));
1199             if (dst != NULL)
1200             {
1201                 // We are decoding a float into host byte order below, so make
1202                 // sure m_data knows what it contains.
1203                 m_data.SetByteOrder(lldb::endian::InlHostByteOrder());
1204                 const size_t converted_byte_size = ClangASTContext::ConvertStringToFloatValue (
1205                                                         GetClangAST(),
1206                                                         GetClangType(),
1207                                                         value_str,
1208                                                         dst,
1209                                                         byte_size);
1210 
1211                 if (converted_byte_size == byte_size)
1212                 {
1213                 }
1214             }
1215         }
1216         break;
1217 
1218     case eEncodingVector:
1219         return false;
1220 
1221     default:
1222         return false;
1223     }
1224 
1225     // If we have made it here the value is in m_data and we should write it
1226     // out to the target
1227     return Write ();
1228 }
1229 
1230 bool
1231 ValueObject::Write ()
1232 {
1233     // Clear the update ID so the next time we try and read the value
1234     // we try and read it again.
1235     m_update_point.SetNeedsUpdate();
1236 
1237     // TODO: when Value has a method to write a value back, call it from here.
1238     return false;
1239 
1240 }
1241 
1242 lldb::LanguageType
1243 ValueObject::GetObjectRuntimeLanguage ()
1244 {
1245     clang_type_t opaque_qual_type = GetClangType();
1246     if (opaque_qual_type == NULL)
1247         return lldb::eLanguageTypeC;
1248 
1249     // If the type is a reference, then resolve it to what it refers to first:
1250     clang::QualType qual_type (clang::QualType::getFromOpaquePtr(opaque_qual_type).getNonReferenceType());
1251     if (qual_type->isAnyPointerType())
1252     {
1253         if (qual_type->isObjCObjectPointerType())
1254             return lldb::eLanguageTypeObjC;
1255 
1256         clang::QualType pointee_type (qual_type->getPointeeType());
1257         if (pointee_type->getCXXRecordDeclForPointerType() != NULL)
1258             return lldb::eLanguageTypeC_plus_plus;
1259         if (pointee_type->isObjCObjectOrInterfaceType())
1260             return lldb::eLanguageTypeObjC;
1261         if (pointee_type->isObjCClassType())
1262             return lldb::eLanguageTypeObjC;
1263     }
1264     else
1265     {
1266         if (ClangASTContext::IsObjCClassType (opaque_qual_type))
1267             return lldb::eLanguageTypeObjC;
1268         if (ClangASTContext::IsCXXClassType (opaque_qual_type))
1269             return lldb::eLanguageTypeC_plus_plus;
1270     }
1271 
1272     return lldb::eLanguageTypeC;
1273 }
1274 
1275 void
1276 ValueObject::AddSyntheticChild (const ConstString &key, ValueObject *valobj)
1277 {
1278     m_synthetic_children[key] = valobj;
1279 }
1280 
1281 ValueObjectSP
1282 ValueObject::GetSyntheticChild (const ConstString &key) const
1283 {
1284     ValueObjectSP synthetic_child_sp;
1285     std::map<ConstString, ValueObject *>::const_iterator pos = m_synthetic_children.find (key);
1286     if (pos != m_synthetic_children.end())
1287         synthetic_child_sp = pos->second->GetSP();
1288     return synthetic_child_sp;
1289 }
1290 
1291 bool
1292 ValueObject::IsPointerType ()
1293 {
1294     return ClangASTContext::IsPointerType (GetClangType());
1295 }
1296 
1297 bool
1298 ValueObject::IsArrayType ()
1299 {
1300     return ClangASTContext::IsArrayType (GetClangType());
1301 }
1302 
1303 bool
1304 ValueObject::IsScalarType ()
1305 {
1306     return ClangASTContext::IsScalarType (GetClangType());
1307 }
1308 
1309 bool
1310 ValueObject::IsIntegerType (bool &is_signed)
1311 {
1312     return ClangASTContext::IsIntegerType (GetClangType(), is_signed);
1313 }
1314 
1315 bool
1316 ValueObject::IsPointerOrReferenceType ()
1317 {
1318     return ClangASTContext::IsPointerOrReferenceType (GetClangType());
1319 }
1320 
1321 bool
1322 ValueObject::IsPossibleCPlusPlusDynamicType ()
1323 {
1324     return ClangASTContext::IsPossibleCPlusPlusDynamicType (GetClangAST (), GetClangType());
1325 }
1326 
1327 bool
1328 ValueObject::IsPossibleDynamicType ()
1329 {
1330     return ClangASTContext::IsPossibleDynamicType (GetClangAST (), GetClangType());
1331 }
1332 
1333 ValueObjectSP
1334 ValueObject::GetSyntheticArrayMemberFromPointer (int32_t index, bool can_create)
1335 {
1336     ValueObjectSP synthetic_child_sp;
1337     if (IsPointerType ())
1338     {
1339         char index_str[64];
1340         snprintf(index_str, sizeof(index_str), "[%i]", index);
1341         ConstString index_const_str(index_str);
1342         // Check if we have already created a synthetic array member in this
1343         // valid object. If we have we will re-use it.
1344         synthetic_child_sp = GetSyntheticChild (index_const_str);
1345         if (!synthetic_child_sp)
1346         {
1347             ValueObject *synthetic_child;
1348             // We haven't made a synthetic array member for INDEX yet, so
1349             // lets make one and cache it for any future reference.
1350             synthetic_child = CreateChildAtIndex(0, true, index);
1351 
1352             // Cache the value if we got one back...
1353             if (synthetic_child)
1354             {
1355                 AddSyntheticChild(index_const_str, synthetic_child);
1356                 synthetic_child_sp = synthetic_child->GetSP();
1357                 synthetic_child_sp->SetName(index_str);
1358                 synthetic_child_sp->m_is_array_item_for_pointer = true;
1359             }
1360         }
1361     }
1362     return synthetic_child_sp;
1363 }
1364 
1365 // This allows you to create an array member using and index
1366 // that doesn't not fall in the normal bounds of the array.
1367 // Many times structure can be defined as:
1368 // struct Collection
1369 // {
1370 //     uint32_t item_count;
1371 //     Item item_array[0];
1372 // };
1373 // The size of the "item_array" is 1, but many times in practice
1374 // there are more items in "item_array".
1375 
1376 ValueObjectSP
1377 ValueObject::GetSyntheticArrayMemberFromArray (int32_t index, bool can_create)
1378 {
1379     ValueObjectSP synthetic_child_sp;
1380     if (IsArrayType ())
1381     {
1382         char index_str[64];
1383         snprintf(index_str, sizeof(index_str), "[%i]", index);
1384         ConstString index_const_str(index_str);
1385         // Check if we have already created a synthetic array member in this
1386         // valid object. If we have we will re-use it.
1387         synthetic_child_sp = GetSyntheticChild (index_const_str);
1388         if (!synthetic_child_sp)
1389         {
1390             ValueObject *synthetic_child;
1391             // We haven't made a synthetic array member for INDEX yet, so
1392             // lets make one and cache it for any future reference.
1393             synthetic_child = CreateChildAtIndex(0, true, index);
1394 
1395             // Cache the value if we got one back...
1396             if (synthetic_child)
1397             {
1398                 AddSyntheticChild(index_const_str, synthetic_child);
1399                 synthetic_child_sp = synthetic_child->GetSP();
1400                 synthetic_child_sp->SetName(index_str);
1401                 synthetic_child_sp->m_is_array_item_for_pointer = true;
1402             }
1403         }
1404     }
1405     return synthetic_child_sp;
1406 }
1407 
1408 ValueObjectSP
1409 ValueObject::GetSyntheticBitFieldChild (uint32_t from, uint32_t to, bool can_create)
1410 {
1411     ValueObjectSP synthetic_child_sp;
1412     if (IsScalarType ())
1413     {
1414         char index_str[64];
1415         snprintf(index_str, sizeof(index_str), "[%i-%i]", from, to);
1416         ConstString index_const_str(index_str);
1417         // Check if we have already created a synthetic array member in this
1418         // valid object. If we have we will re-use it.
1419         synthetic_child_sp = GetSyntheticChild (index_const_str);
1420         if (!synthetic_child_sp)
1421         {
1422             ValueObjectChild *synthetic_child;
1423             // We haven't made a synthetic array member for INDEX yet, so
1424             // lets make one and cache it for any future reference.
1425             synthetic_child = new ValueObjectChild(*this,
1426                                                       GetClangAST(),
1427                                                       GetClangType(),
1428                                                       index_const_str,
1429                                                       GetByteSize(),
1430                                                       0,
1431                                                       to-from+1,
1432                                                       from,
1433                                                       false,
1434                                                       false);
1435 
1436             // Cache the value if we got one back...
1437             if (synthetic_child)
1438             {
1439                 AddSyntheticChild(index_const_str, synthetic_child);
1440                 synthetic_child_sp = synthetic_child->GetSP();
1441                 synthetic_child_sp->SetName(index_str);
1442                 synthetic_child_sp->m_is_bitfield_for_scalar = true;
1443             }
1444         }
1445     }
1446     return synthetic_child_sp;
1447 }
1448 
1449 void
1450 ValueObject::CalculateDynamicValue (lldb::DynamicValueType use_dynamic)
1451 {
1452     if (use_dynamic == lldb::eNoDynamicValues)
1453         return;
1454 
1455     if (!m_dynamic_value && !IsDynamic())
1456     {
1457         Process *process = m_update_point.GetProcess();
1458         bool worth_having_dynamic_value = false;
1459 
1460 
1461         // FIXME: Process should have some kind of "map over Runtimes" so we don't have to
1462         // hard code this everywhere.
1463         lldb::LanguageType known_type = GetObjectRuntimeLanguage();
1464         if (known_type != lldb::eLanguageTypeUnknown && known_type != lldb::eLanguageTypeC)
1465         {
1466             LanguageRuntime *runtime = process->GetLanguageRuntime (known_type);
1467             if (runtime)
1468                 worth_having_dynamic_value = runtime->CouldHaveDynamicValue(*this);
1469         }
1470         else
1471         {
1472             LanguageRuntime *cpp_runtime = process->GetLanguageRuntime (lldb::eLanguageTypeC_plus_plus);
1473             if (cpp_runtime)
1474                 worth_having_dynamic_value = cpp_runtime->CouldHaveDynamicValue(*this);
1475 
1476             if (!worth_having_dynamic_value)
1477             {
1478                 LanguageRuntime *objc_runtime = process->GetLanguageRuntime (lldb::eLanguageTypeObjC);
1479                 if (objc_runtime)
1480                     worth_having_dynamic_value = objc_runtime->CouldHaveDynamicValue(*this);
1481             }
1482         }
1483 
1484         if (worth_having_dynamic_value)
1485             m_dynamic_value = new ValueObjectDynamicValue (*this, use_dynamic);
1486 
1487 //        if (worth_having_dynamic_value)
1488 //            printf ("Adding dynamic value %s (%p) to (%p) - manager %p.\n", m_name.GetCString(), m_dynamic_value, this, m_manager);
1489 
1490     }
1491 }
1492 
1493 ValueObjectSP
1494 ValueObject::GetDynamicValue (DynamicValueType use_dynamic)
1495 {
1496     if (use_dynamic == lldb::eNoDynamicValues)
1497         return ValueObjectSP();
1498 
1499     if (!IsDynamic() && m_dynamic_value == NULL)
1500     {
1501         CalculateDynamicValue(use_dynamic);
1502     }
1503     if (m_dynamic_value)
1504         return m_dynamic_value->GetSP();
1505     else
1506         return ValueObjectSP();
1507 }
1508 
1509 bool
1510 ValueObject::GetBaseClassPath (Stream &s)
1511 {
1512     if (IsBaseClass())
1513     {
1514         bool parent_had_base_class = GetParent() && GetParent()->GetBaseClassPath (s);
1515         clang_type_t clang_type = GetClangType();
1516         std::string cxx_class_name;
1517         bool this_had_base_class = ClangASTContext::GetCXXClassName (clang_type, cxx_class_name);
1518         if (this_had_base_class)
1519         {
1520             if (parent_had_base_class)
1521                 s.PutCString("::");
1522             s.PutCString(cxx_class_name.c_str());
1523         }
1524         return parent_had_base_class || this_had_base_class;
1525     }
1526     return false;
1527 }
1528 
1529 
1530 ValueObject *
1531 ValueObject::GetNonBaseClassParent()
1532 {
1533     if (GetParent())
1534     {
1535         if (GetParent()->IsBaseClass())
1536             return GetParent()->GetNonBaseClassParent();
1537         else
1538             return GetParent();
1539     }
1540     return NULL;
1541 }
1542 
1543 void
1544 ValueObject::GetExpressionPath (Stream &s, bool qualify_cxx_base_classes, GetExpressionPathFormat epformat)
1545 {
1546     const bool is_deref_of_parent = IsDereferenceOfParent ();
1547 
1548     if(is_deref_of_parent && epformat == eDereferencePointers) {
1549         // this is the original format of GetExpressionPath() producing code like *(a_ptr).memberName, which is entirely
1550         // fine, until you put this into StackFrame::GetValueForVariableExpressionPath() which prefers to see a_ptr->memberName.
1551         // the eHonorPointers mode is meant to produce strings in this latter format
1552         s.PutCString("*(");
1553     }
1554 
1555     ValueObject* parent = GetParent();
1556 
1557     if (parent)
1558         parent->GetExpressionPath (s, qualify_cxx_base_classes, epformat);
1559 
1560     // if we are a deref_of_parent just because we are synthetic array
1561     // members made up to allow ptr[%d] syntax to work in variable
1562     // printing, then add our name ([%d]) to the expression path
1563     if(m_is_array_item_for_pointer && epformat == eHonorPointers)
1564         s.PutCString(m_name.AsCString());
1565 
1566     if (!IsBaseClass())
1567     {
1568         if (!is_deref_of_parent)
1569         {
1570             ValueObject *non_base_class_parent = GetNonBaseClassParent();
1571             if (non_base_class_parent)
1572             {
1573                 clang_type_t non_base_class_parent_clang_type = non_base_class_parent->GetClangType();
1574                 if (non_base_class_parent_clang_type)
1575                 {
1576                     const uint32_t non_base_class_parent_type_info = ClangASTContext::GetTypeInfo (non_base_class_parent_clang_type, NULL, NULL);
1577 
1578                     if(parent && parent->IsDereferenceOfParent() && epformat == eHonorPointers)
1579                     {
1580                         s.PutCString("->");
1581                     }
1582                     else
1583                     {
1584                         if (non_base_class_parent_type_info & ClangASTContext::eTypeIsPointer)
1585                         {
1586                             s.PutCString("->");
1587                         }
1588                         else if ((non_base_class_parent_type_info & ClangASTContext::eTypeHasChildren) &&
1589                                  !(non_base_class_parent_type_info & ClangASTContext::eTypeIsArray))
1590                         {
1591                             s.PutChar('.');
1592                         }
1593                     }
1594                 }
1595             }
1596 
1597             const char *name = GetName().GetCString();
1598             if (name)
1599             {
1600                 if (qualify_cxx_base_classes)
1601                 {
1602                     if (GetBaseClassPath (s))
1603                         s.PutCString("::");
1604                 }
1605                 s.PutCString(name);
1606             }
1607         }
1608     }
1609 
1610     if (is_deref_of_parent && epformat == eDereferencePointers) {
1611         s.PutChar(')');
1612     }
1613 }
1614 
1615 lldb::ValueObjectSP
1616 ValueObject::GetValueForExpressionPath(const char* expression,
1617                                        const char** first_unparsed,
1618                                        ExpressionPathScanEndReason* reason_to_stop,
1619                                        ExpressionPathEndResultType* final_value_type,
1620                                        const GetValueForExpressionPathOptions& options,
1621                                        ExpressionPathAftermath* final_task_on_target)
1622 {
1623 
1624     const char* dummy_first_unparsed;
1625     ExpressionPathScanEndReason dummy_reason_to_stop;
1626     ExpressionPathEndResultType dummy_final_value_type;
1627     ExpressionPathAftermath dummy_final_task_on_target = ValueObject::eNothing;
1628 
1629     ValueObjectSP ret_val = GetValueForExpressionPath_Impl(expression,
1630                                                            first_unparsed ? first_unparsed : &dummy_first_unparsed,
1631                                                            reason_to_stop ? reason_to_stop : &dummy_reason_to_stop,
1632                                                            final_value_type ? final_value_type : &dummy_final_value_type,
1633                                                            options,
1634                                                            final_task_on_target ? final_task_on_target : &dummy_final_task_on_target);
1635 
1636     if (!final_task_on_target || *final_task_on_target == ValueObject::eNothing)
1637     {
1638         return ret_val;
1639     }
1640     if (ret_val.get() && *final_value_type == ePlain) // I can only deref and takeaddress of plain objects
1641     {
1642         if (*final_task_on_target == ValueObject::eDereference)
1643         {
1644             Error error;
1645             ValueObjectSP final_value = ret_val->Dereference(error);
1646             if (error.Fail() || !final_value.get())
1647             {
1648                 *reason_to_stop = ValueObject::eDereferencingFailed;
1649                 *final_value_type = ValueObject::eInvalid;
1650                 return ValueObjectSP();
1651             }
1652             else
1653             {
1654                 *final_task_on_target = ValueObject::eNothing;
1655                 return final_value;
1656             }
1657         }
1658         if (*final_task_on_target == ValueObject::eTakeAddress)
1659         {
1660             Error error;
1661             ValueObjectSP final_value = ret_val->AddressOf(error);
1662             if (error.Fail() || !final_value.get())
1663             {
1664                 *reason_to_stop = ValueObject::eTakingAddressFailed;
1665                 *final_value_type = ValueObject::eInvalid;
1666                 return ValueObjectSP();
1667             }
1668             else
1669             {
1670                 *final_task_on_target = ValueObject::eNothing;
1671                 return final_value;
1672             }
1673         }
1674     }
1675     return ret_val; // final_task_on_target will still have its original value, so you know I did not do it
1676 }
1677 
1678 int
1679 ValueObject::GetValuesForExpressionPath(const char* expression,
1680                                         lldb::ValueObjectListSP& list,
1681                                         const char** first_unparsed,
1682                                         ExpressionPathScanEndReason* reason_to_stop,
1683                                         ExpressionPathEndResultType* final_value_type,
1684                                         const GetValueForExpressionPathOptions& options,
1685                                         ExpressionPathAftermath* final_task_on_target)
1686 {
1687     const char* dummy_first_unparsed;
1688     ExpressionPathScanEndReason dummy_reason_to_stop;
1689     ExpressionPathEndResultType dummy_final_value_type;
1690     ExpressionPathAftermath dummy_final_task_on_target = ValueObject::eNothing;
1691 
1692     ValueObjectSP ret_val = GetValueForExpressionPath_Impl(expression,
1693                                                            first_unparsed ? first_unparsed : &dummy_first_unparsed,
1694                                                            reason_to_stop ? reason_to_stop : &dummy_reason_to_stop,
1695                                                            final_value_type ? final_value_type : &dummy_final_value_type,
1696                                                            options,
1697                                                            final_task_on_target ? final_task_on_target : &dummy_final_task_on_target);
1698 
1699     if (!ret_val.get()) // if there are errors, I add nothing to the list
1700         return 0;
1701 
1702     if (*reason_to_stop != eArrayRangeOperatorMet)
1703     {
1704         // I need not expand a range, just post-process the final value and return
1705         if (!final_task_on_target || *final_task_on_target == ValueObject::eNothing)
1706         {
1707             list->Append(ret_val);
1708             return 1;
1709         }
1710         if (ret_val.get() && *final_value_type == ePlain) // I can only deref and takeaddress of plain objects
1711         {
1712             if (*final_task_on_target == ValueObject::eDereference)
1713             {
1714                 Error error;
1715                 ValueObjectSP final_value = ret_val->Dereference(error);
1716                 if (error.Fail() || !final_value.get())
1717                 {
1718                     *reason_to_stop = ValueObject::eDereferencingFailed;
1719                     *final_value_type = ValueObject::eInvalid;
1720                     return 0;
1721                 }
1722                 else
1723                 {
1724                     *final_task_on_target = ValueObject::eNothing;
1725                     list->Append(final_value);
1726                     return 1;
1727                 }
1728             }
1729             if (*final_task_on_target == ValueObject::eTakeAddress)
1730             {
1731                 Error error;
1732                 ValueObjectSP final_value = ret_val->AddressOf(error);
1733                 if (error.Fail() || !final_value.get())
1734                 {
1735                     *reason_to_stop = ValueObject::eTakingAddressFailed;
1736                     *final_value_type = ValueObject::eInvalid;
1737                     return 0;
1738                 }
1739                 else
1740                 {
1741                     *final_task_on_target = ValueObject::eNothing;
1742                     list->Append(final_value);
1743                     return 1;
1744                 }
1745             }
1746         }
1747     }
1748     else
1749     {
1750         return ExpandArraySliceExpression(first_unparsed ? *first_unparsed : dummy_first_unparsed,
1751                                           first_unparsed ? first_unparsed : &dummy_first_unparsed,
1752                                           ret_val,
1753                                           list,
1754                                           reason_to_stop ? reason_to_stop : &dummy_reason_to_stop,
1755                                           final_value_type ? final_value_type : &dummy_final_value_type,
1756                                           options,
1757                                           final_task_on_target ? final_task_on_target : &dummy_final_task_on_target);
1758     }
1759     // in any non-covered case, just do the obviously right thing
1760     list->Append(ret_val);
1761     return 1;
1762 }
1763 
1764 lldb::ValueObjectSP
1765 ValueObject::GetValueForExpressionPath_Impl(const char* expression_cstr,
1766                                             const char** first_unparsed,
1767                                             ExpressionPathScanEndReason* reason_to_stop,
1768                                             ExpressionPathEndResultType* final_result,
1769                                             const GetValueForExpressionPathOptions& options,
1770                                             ExpressionPathAftermath* what_next)
1771 {
1772     ValueObjectSP root = GetSP();
1773 
1774     if (!root.get())
1775         return ValueObjectSP();
1776 
1777     *first_unparsed = expression_cstr;
1778 
1779     while (true)
1780     {
1781 
1782         const char* expression_cstr = *first_unparsed; // hide the top level expression_cstr
1783 
1784         lldb::clang_type_t root_clang_type = root->GetClangType();
1785         lldb::clang_type_t pointee_clang_type;
1786         Flags root_clang_type_info,pointee_clang_type_info;
1787 
1788         root_clang_type_info = Flags(ClangASTContext::GetTypeInfo(root_clang_type, GetClangAST(), &pointee_clang_type));
1789         if (pointee_clang_type)
1790             pointee_clang_type_info = Flags(ClangASTContext::GetTypeInfo(pointee_clang_type, GetClangAST(), NULL));
1791 
1792         if (!expression_cstr || *expression_cstr == '\0')
1793         {
1794             *reason_to_stop = ValueObject::eEndOfString;
1795             return root;
1796         }
1797 
1798         switch (*expression_cstr)
1799         {
1800             case '-':
1801             {
1802                 if (options.m_check_dot_vs_arrow_syntax &&
1803                     root_clang_type_info.Test(ClangASTContext::eTypeIsPointer) ) // if you are trying to use -> on a non-pointer and I must catch the error
1804                 {
1805                     *first_unparsed = expression_cstr;
1806                     *reason_to_stop = ValueObject::eArrowInsteadOfDot;
1807                     *final_result = ValueObject::eInvalid;
1808                     return ValueObjectSP();
1809                 }
1810                 if (root_clang_type_info.Test(ClangASTContext::eTypeIsObjC) &&  // if yo are trying to extract an ObjC IVar when this is forbidden
1811                     root_clang_type_info.Test(ClangASTContext::eTypeIsPointer) &&
1812                     options.m_no_fragile_ivar)
1813                 {
1814                     *first_unparsed = expression_cstr;
1815                     *reason_to_stop = ValueObject::eFragileIVarNotAllowed;
1816                     *final_result = ValueObject::eInvalid;
1817                     return ValueObjectSP();
1818                 }
1819                 if (expression_cstr[1] != '>')
1820                 {
1821                     *first_unparsed = expression_cstr;
1822                     *reason_to_stop = ValueObject::eUnexpectedSymbol;
1823                     *final_result = ValueObject::eInvalid;
1824                     return ValueObjectSP();
1825                 }
1826                 expression_cstr++; // skip the -
1827             }
1828             case '.': // or fallthrough from ->
1829             {
1830                 if (options.m_check_dot_vs_arrow_syntax && *expression_cstr == '.' &&
1831                     root_clang_type_info.Test(ClangASTContext::eTypeIsPointer)) // if you are trying to use . on a pointer and I must catch the error
1832                 {
1833                     *first_unparsed = expression_cstr;
1834                     *reason_to_stop = ValueObject::eDotInsteadOfArrow;
1835                     *final_result = ValueObject::eInvalid;
1836                     return ValueObjectSP();
1837                 }
1838                 expression_cstr++; // skip .
1839                 const char *next_separator = strpbrk(expression_cstr+1,"-.[");
1840                 ConstString child_name;
1841                 if (!next_separator) // if no other separator just expand this last layer
1842                 {
1843                     child_name.SetCString (expression_cstr);
1844                     root = root->GetChildMemberWithName(child_name, true);
1845                     if (root.get()) // we know we are done, so just return
1846                     {
1847                         *first_unparsed = '\0';
1848                         *reason_to_stop = ValueObject::eEndOfString;
1849                         *final_result = ValueObject::ePlain;
1850                         return root;
1851                     }
1852                     else
1853                     {
1854                         *first_unparsed = expression_cstr;
1855                         *reason_to_stop = ValueObject::eNoSuchChild;
1856                         *final_result = ValueObject::eInvalid;
1857                         return ValueObjectSP();
1858                     }
1859                 }
1860                 else // other layers do expand
1861                 {
1862                     child_name.SetCStringWithLength(expression_cstr, next_separator - expression_cstr);
1863                     root = root->GetChildMemberWithName(child_name, true);
1864                     if (root.get()) // store the new root and move on
1865                     {
1866                         *first_unparsed = next_separator;
1867                         *final_result = ValueObject::ePlain;
1868                         continue;
1869                     }
1870                     else
1871                     {
1872                         *first_unparsed = expression_cstr;
1873                         *reason_to_stop = ValueObject::eNoSuchChild;
1874                         *final_result = ValueObject::eInvalid;
1875                         return ValueObjectSP();
1876                     }
1877                 }
1878                 break;
1879             }
1880             case '[':
1881             {
1882                 if (!root_clang_type_info.Test(ClangASTContext::eTypeIsArray) && !root_clang_type_info.Test(ClangASTContext::eTypeIsPointer)) // if this is not a T[] nor a T*
1883                 {
1884                     if (!root_clang_type_info.Test(ClangASTContext::eTypeIsScalar)) // if this is not even a scalar, this syntax is just plain wrong!
1885                     {
1886                         *first_unparsed = expression_cstr;
1887                         *reason_to_stop = ValueObject::eRangeOperatorInvalid;
1888                         *final_result = ValueObject::eInvalid;
1889                         return ValueObjectSP();
1890                     }
1891                     else if (!options.m_allow_bitfields_syntax) // if this is a scalar, check that we can expand bitfields
1892                     {
1893                         *first_unparsed = expression_cstr;
1894                         *reason_to_stop = ValueObject::eRangeOperatorNotAllowed;
1895                         *final_result = ValueObject::eInvalid;
1896                         return ValueObjectSP();
1897                     }
1898                 }
1899                 if (*(expression_cstr+1) == ']') // if this is an unbounded range it only works for arrays
1900                 {
1901                     if (!root_clang_type_info.Test(ClangASTContext::eTypeIsArray))
1902                     {
1903                         *first_unparsed = expression_cstr;
1904                         *reason_to_stop = ValueObject::eEmptyRangeNotAllowed;
1905                         *final_result = ValueObject::eInvalid;
1906                         return ValueObjectSP();
1907                     }
1908                     else // even if something follows, we cannot expand unbounded ranges, just let the caller do it
1909                     {
1910                         *first_unparsed = expression_cstr+2;
1911                         *reason_to_stop = ValueObject::eArrayRangeOperatorMet;
1912                         *final_result = ValueObject::eUnboundedRange;
1913                         return root;
1914                     }
1915                 }
1916                 const char *separator_position = ::strchr(expression_cstr+1,'-');
1917                 const char *close_bracket_position = ::strchr(expression_cstr+1,']');
1918                 if (!close_bracket_position) // if there is no ], this is a syntax error
1919                 {
1920                     *first_unparsed = expression_cstr;
1921                     *reason_to_stop = ValueObject::eUnexpectedSymbol;
1922                     *final_result = ValueObject::eInvalid;
1923                     return ValueObjectSP();
1924                 }
1925                 if (!separator_position || separator_position > close_bracket_position) // if no separator, this is either [] or [N]
1926                 {
1927                     char *end = NULL;
1928                     unsigned long index = ::strtoul (expression_cstr+1, &end, 0);
1929                     if (!end || end != close_bracket_position) // if something weird is in our way return an error
1930                     {
1931                         *first_unparsed = expression_cstr;
1932                         *reason_to_stop = ValueObject::eUnexpectedSymbol;
1933                         *final_result = ValueObject::eInvalid;
1934                         return ValueObjectSP();
1935                     }
1936                     if (end - expression_cstr == 1) // if this is [], only return a valid value for arrays
1937                     {
1938                         if (root_clang_type_info.Test(ClangASTContext::eTypeIsArray))
1939                         {
1940                             *first_unparsed = expression_cstr+2;
1941                             *reason_to_stop = ValueObject::eArrayRangeOperatorMet;
1942                             *final_result = ValueObject::eUnboundedRange;
1943                             return root;
1944                         }
1945                         else
1946                         {
1947                             *first_unparsed = expression_cstr;
1948                             *reason_to_stop = ValueObject::eEmptyRangeNotAllowed;
1949                             *final_result = ValueObject::eInvalid;
1950                             return ValueObjectSP();
1951                         }
1952                     }
1953                     // from here on we do have a valid index
1954                     if (root_clang_type_info.Test(ClangASTContext::eTypeIsArray))
1955                     {
1956                         ValueObjectSP child_valobj_sp = root->GetChildAtIndex(index, true);
1957                         if (!child_valobj_sp)
1958                             child_valobj_sp = root->GetSyntheticArrayMemberFromArray(index, true);
1959                         if (child_valobj_sp)
1960                         {
1961                             root = child_valobj_sp;
1962                             *first_unparsed = end+1; // skip ]
1963                             *final_result = ValueObject::ePlain;
1964                             continue;
1965                         }
1966                         else
1967                         {
1968                             *first_unparsed = expression_cstr;
1969                             *reason_to_stop = ValueObject::eNoSuchChild;
1970                             *final_result = ValueObject::eInvalid;
1971                             return ValueObjectSP();
1972                         }
1973                     }
1974                     else if (root_clang_type_info.Test(ClangASTContext::eTypeIsPointer))
1975                     {
1976                         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
1977                             pointee_clang_type_info.Test(ClangASTContext::eTypeIsScalar))
1978                         {
1979                             Error error;
1980                             root = root->Dereference(error);
1981                             if (error.Fail() || !root.get())
1982                             {
1983                                 *first_unparsed = expression_cstr;
1984                                 *reason_to_stop = ValueObject::eDereferencingFailed;
1985                                 *final_result = ValueObject::eInvalid;
1986                                 return ValueObjectSP();
1987                             }
1988                             else
1989                             {
1990                                 *what_next = eNothing;
1991                                 continue;
1992                             }
1993                         }
1994                         else
1995                         {
1996                             root = root->GetSyntheticArrayMemberFromPointer(index, true);
1997                             if (!root.get())
1998                             {
1999                                 *first_unparsed = expression_cstr;
2000                                 *reason_to_stop = ValueObject::eNoSuchChild;
2001                                 *final_result = ValueObject::eInvalid;
2002                                 return ValueObjectSP();
2003                             }
2004                             else
2005                             {
2006                                 *first_unparsed = end+1; // skip ]
2007                                 *final_result = ValueObject::ePlain;
2008                                 continue;
2009                             }
2010                         }
2011                     }
2012                     else /*if (ClangASTContext::IsScalarType(root_clang_type))*/
2013                     {
2014                         root = root->GetSyntheticBitFieldChild(index, index, true);
2015                         if (!root.get())
2016                         {
2017                             *first_unparsed = expression_cstr;
2018                             *reason_to_stop = ValueObject::eNoSuchChild;
2019                             *final_result = ValueObject::eInvalid;
2020                             return ValueObjectSP();
2021                         }
2022                         else // we do not know how to expand members of bitfields, so we just return and let the caller do any further processing
2023                         {
2024                             *first_unparsed = end+1; // skip ]
2025                             *reason_to_stop = ValueObject::eBitfieldRangeOperatorMet;
2026                             *final_result = ValueObject::eBitfield;
2027                             return root;
2028                         }
2029                     }
2030                 }
2031                 else // we have a low and a high index
2032                 {
2033                     char *end = NULL;
2034                     unsigned long index_lower = ::strtoul (expression_cstr+1, &end, 0);
2035                     if (!end || end != separator_position) // if something weird is in our way return an error
2036                     {
2037                         *first_unparsed = expression_cstr;
2038                         *reason_to_stop = ValueObject::eUnexpectedSymbol;
2039                         *final_result = ValueObject::eInvalid;
2040                         return ValueObjectSP();
2041                     }
2042                     unsigned long index_higher = ::strtoul (separator_position+1, &end, 0);
2043                     if (!end || end != close_bracket_position) // if something weird is in our way return an error
2044                     {
2045                         *first_unparsed = expression_cstr;
2046                         *reason_to_stop = ValueObject::eUnexpectedSymbol;
2047                         *final_result = ValueObject::eInvalid;
2048                         return ValueObjectSP();
2049                     }
2050                     if (index_lower > index_higher) // swap indices if required
2051                     {
2052                         unsigned long temp = index_lower;
2053                         index_lower = index_higher;
2054                         index_higher = temp;
2055                     }
2056                     if (root_clang_type_info.Test(ClangASTContext::eTypeIsScalar)) // expansion only works for scalars
2057                     {
2058                         root = root->GetSyntheticBitFieldChild(index_lower, index_higher, true);
2059                         if (!root.get())
2060                         {
2061                             *first_unparsed = expression_cstr;
2062                             *reason_to_stop = ValueObject::eNoSuchChild;
2063                             *final_result = ValueObject::eInvalid;
2064                             return ValueObjectSP();
2065                         }
2066                         else
2067                         {
2068                             *first_unparsed = end+1; // skip ]
2069                             *reason_to_stop = ValueObject::eBitfieldRangeOperatorMet;
2070                             *final_result = ValueObject::eBitfield;
2071                             return root;
2072                         }
2073                     }
2074                     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
2075                              *what_next == ValueObject::eDereference &&
2076                              pointee_clang_type_info.Test(ClangASTContext::eTypeIsScalar))
2077                     {
2078                         Error error;
2079                         root = root->Dereference(error);
2080                         if (error.Fail() || !root.get())
2081                         {
2082                             *first_unparsed = expression_cstr;
2083                             *reason_to_stop = ValueObject::eDereferencingFailed;
2084                             *final_result = ValueObject::eInvalid;
2085                             return ValueObjectSP();
2086                         }
2087                         else
2088                         {
2089                             *what_next = ValueObject::eNothing;
2090                             continue;
2091                         }
2092                     }
2093                     else
2094                     {
2095                         *first_unparsed = expression_cstr;
2096                         *reason_to_stop = ValueObject::eArrayRangeOperatorMet;
2097                         *final_result = ValueObject::eBoundedRange;
2098                         return root;
2099                     }
2100                 }
2101                 break;
2102             }
2103             default: // some non-separator is in the way
2104             {
2105                 *first_unparsed = expression_cstr;
2106                 *reason_to_stop = ValueObject::eUnexpectedSymbol;
2107                 *final_result = ValueObject::eInvalid;
2108                 return ValueObjectSP();
2109                 break;
2110             }
2111         }
2112     }
2113 }
2114 
2115 int
2116 ValueObject::ExpandArraySliceExpression(const char* expression_cstr,
2117                                         const char** first_unparsed,
2118                                         lldb::ValueObjectSP root,
2119                                         lldb::ValueObjectListSP& list,
2120                                         ExpressionPathScanEndReason* reason_to_stop,
2121                                         ExpressionPathEndResultType* final_result,
2122                                         const GetValueForExpressionPathOptions& options,
2123                                         ExpressionPathAftermath* what_next)
2124 {
2125     if (!root.get())
2126         return 0;
2127 
2128     *first_unparsed = expression_cstr;
2129 
2130     while (true)
2131     {
2132 
2133         const char* expression_cstr = *first_unparsed; // hide the top level expression_cstr
2134 
2135         lldb::clang_type_t root_clang_type = root->GetClangType();
2136         lldb::clang_type_t pointee_clang_type;
2137         Flags root_clang_type_info,pointee_clang_type_info;
2138 
2139         root_clang_type_info = Flags(ClangASTContext::GetTypeInfo(root_clang_type, GetClangAST(), &pointee_clang_type));
2140         if (pointee_clang_type)
2141             pointee_clang_type_info = Flags(ClangASTContext::GetTypeInfo(pointee_clang_type, GetClangAST(), NULL));
2142 
2143         if (!expression_cstr || *expression_cstr == '\0')
2144         {
2145             *reason_to_stop = ValueObject::eEndOfString;
2146             list->Append(root);
2147             return 1;
2148         }
2149 
2150         switch (*expression_cstr)
2151         {
2152             case '[':
2153             {
2154                 if (!root_clang_type_info.Test(ClangASTContext::eTypeIsArray) && !root_clang_type_info.Test(ClangASTContext::eTypeIsPointer)) // if this is not a T[] nor a T*
2155                 {
2156                     if (!root_clang_type_info.Test(ClangASTContext::eTypeIsScalar)) // if this is not even a scalar, this syntax is just plain wrong!
2157                     {
2158                         *first_unparsed = expression_cstr;
2159                         *reason_to_stop = ValueObject::eRangeOperatorInvalid;
2160                         *final_result = ValueObject::eInvalid;
2161                         return 0;
2162                     }
2163                     else if (!options.m_allow_bitfields_syntax) // if this is a scalar, check that we can expand bitfields
2164                     {
2165                         *first_unparsed = expression_cstr;
2166                         *reason_to_stop = ValueObject::eRangeOperatorNotAllowed;
2167                         *final_result = ValueObject::eInvalid;
2168                         return 0;
2169                     }
2170                 }
2171                 if (*(expression_cstr+1) == ']') // if this is an unbounded range it only works for arrays
2172                 {
2173                     if (!root_clang_type_info.Test(ClangASTContext::eTypeIsArray))
2174                     {
2175                         *first_unparsed = expression_cstr;
2176                         *reason_to_stop = ValueObject::eEmptyRangeNotAllowed;
2177                         *final_result = ValueObject::eInvalid;
2178                         return 0;
2179                     }
2180                     else // expand this into list
2181                     {
2182                         int max_index = root->GetNumChildren() - 1;
2183                         for (int index = 0; index < max_index; index++)
2184                         {
2185                             ValueObjectSP child =
2186                                 root->GetChildAtIndex(index, true);
2187                             list->Append(child);
2188                         }
2189                         *first_unparsed = expression_cstr+2;
2190                         *reason_to_stop = ValueObject::eRangeOperatorExpanded;
2191                         *final_result = ValueObject::eValueObjectList;
2192                         return max_index; // tell me number of items I added to the VOList
2193                     }
2194                 }
2195                 const char *separator_position = ::strchr(expression_cstr+1,'-');
2196                 const char *close_bracket_position = ::strchr(expression_cstr+1,']');
2197                 if (!close_bracket_position) // if there is no ], this is a syntax error
2198                 {
2199                     *first_unparsed = expression_cstr;
2200                     *reason_to_stop = ValueObject::eUnexpectedSymbol;
2201                     *final_result = ValueObject::eInvalid;
2202                     return 0;
2203                 }
2204                 if (!separator_position || separator_position > close_bracket_position) // if no separator, this is either [] or [N]
2205                 {
2206                     char *end = NULL;
2207                     unsigned long index = ::strtoul (expression_cstr+1, &end, 0);
2208                     if (!end || end != close_bracket_position) // if something weird is in our way return an error
2209                     {
2210                         *first_unparsed = expression_cstr;
2211                         *reason_to_stop = ValueObject::eUnexpectedSymbol;
2212                         *final_result = ValueObject::eInvalid;
2213                         return 0;
2214                     }
2215                     if (end - expression_cstr == 1) // if this is [], only return a valid value for arrays
2216                     {
2217                         if (root_clang_type_info.Test(ClangASTContext::eTypeIsArray))
2218                         {
2219                             int max_index = root->GetNumChildren() - 1;
2220                             for (int index = 0; index < max_index; index++)
2221                             {
2222                                 ValueObjectSP child =
2223                                 root->GetChildAtIndex(index, true);
2224                                 list->Append(child);
2225                             }
2226                             *first_unparsed = expression_cstr+2;
2227                             *reason_to_stop = ValueObject::eRangeOperatorExpanded;
2228                             *final_result = ValueObject::eValueObjectList;
2229                             return max_index; // tell me number of items I added to the VOList
2230                         }
2231                         else
2232                         {
2233                             *first_unparsed = expression_cstr;
2234                             *reason_to_stop = ValueObject::eEmptyRangeNotAllowed;
2235                             *final_result = ValueObject::eInvalid;
2236                             return 0;
2237                         }
2238                     }
2239                     // from here on we do have a valid index
2240                     if (root_clang_type_info.Test(ClangASTContext::eTypeIsArray))
2241                     {
2242                         root = root->GetChildAtIndex(index, true);
2243                         if (!root.get())
2244                         {
2245                             *first_unparsed = expression_cstr;
2246                             *reason_to_stop = ValueObject::eNoSuchChild;
2247                             *final_result = ValueObject::eInvalid;
2248                             return 0;
2249                         }
2250                         else
2251                         {
2252                             list->Append(root);
2253                             *first_unparsed = end+1; // skip ]
2254                             *reason_to_stop = ValueObject::eRangeOperatorExpanded;
2255                             *final_result = ValueObject::eValueObjectList;
2256                             return 1;
2257                         }
2258                     }
2259                     else if (root_clang_type_info.Test(ClangASTContext::eTypeIsPointer))
2260                     {
2261                         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
2262                             pointee_clang_type_info.Test(ClangASTContext::eTypeIsScalar))
2263                         {
2264                             Error error;
2265                             root = root->Dereference(error);
2266                             if (error.Fail() || !root.get())
2267                             {
2268                                 *first_unparsed = expression_cstr;
2269                                 *reason_to_stop = ValueObject::eDereferencingFailed;
2270                                 *final_result = ValueObject::eInvalid;
2271                                 return 0;
2272                             }
2273                             else
2274                             {
2275                                 *what_next = eNothing;
2276                                 continue;
2277                             }
2278                         }
2279                         else
2280                         {
2281                             root = root->GetSyntheticArrayMemberFromPointer(index, true);
2282                             if (!root.get())
2283                             {
2284                                 *first_unparsed = expression_cstr;
2285                                 *reason_to_stop = ValueObject::eNoSuchChild;
2286                                 *final_result = ValueObject::eInvalid;
2287                                 return 0;
2288                             }
2289                             else
2290                             {
2291                                 list->Append(root);
2292                                 *first_unparsed = end+1; // skip ]
2293                                 *reason_to_stop = ValueObject::eRangeOperatorExpanded;
2294                                 *final_result = ValueObject::eValueObjectList;
2295                                 return 1;
2296                             }
2297                         }
2298                     }
2299                     else /*if (ClangASTContext::IsScalarType(root_clang_type))*/
2300                     {
2301                         root = root->GetSyntheticBitFieldChild(index, index, true);
2302                         if (!root.get())
2303                         {
2304                             *first_unparsed = expression_cstr;
2305                             *reason_to_stop = ValueObject::eNoSuchChild;
2306                             *final_result = ValueObject::eInvalid;
2307                             return 0;
2308                         }
2309                         else // we do not know how to expand members of bitfields, so we just return and let the caller do any further processing
2310                         {
2311                             list->Append(root);
2312                             *first_unparsed = end+1; // skip ]
2313                             *reason_to_stop = ValueObject::eRangeOperatorExpanded;
2314                             *final_result = ValueObject::eValueObjectList;
2315                             return 1;
2316                         }
2317                     }
2318                 }
2319                 else // we have a low and a high index
2320                 {
2321                     char *end = NULL;
2322                     unsigned long index_lower = ::strtoul (expression_cstr+1, &end, 0);
2323                     if (!end || end != separator_position) // if something weird is in our way return an error
2324                     {
2325                         *first_unparsed = expression_cstr;
2326                         *reason_to_stop = ValueObject::eUnexpectedSymbol;
2327                         *final_result = ValueObject::eInvalid;
2328                         return 0;
2329                     }
2330                     unsigned long index_higher = ::strtoul (separator_position+1, &end, 0);
2331                     if (!end || end != close_bracket_position) // if something weird is in our way return an error
2332                     {
2333                         *first_unparsed = expression_cstr;
2334                         *reason_to_stop = ValueObject::eUnexpectedSymbol;
2335                         *final_result = ValueObject::eInvalid;
2336                         return 0;
2337                     }
2338                     if (index_lower > index_higher) // swap indices if required
2339                     {
2340                         unsigned long temp = index_lower;
2341                         index_lower = index_higher;
2342                         index_higher = temp;
2343                     }
2344                     if (root_clang_type_info.Test(ClangASTContext::eTypeIsScalar)) // expansion only works for scalars
2345                     {
2346                         root = root->GetSyntheticBitFieldChild(index_lower, index_higher, true);
2347                         if (!root.get())
2348                         {
2349                             *first_unparsed = expression_cstr;
2350                             *reason_to_stop = ValueObject::eNoSuchChild;
2351                             *final_result = ValueObject::eInvalid;
2352                             return 0;
2353                         }
2354                         else
2355                         {
2356                             list->Append(root);
2357                             *first_unparsed = end+1; // skip ]
2358                             *reason_to_stop = ValueObject::eRangeOperatorExpanded;
2359                             *final_result = ValueObject::eValueObjectList;
2360                             return 1;
2361                         }
2362                     }
2363                     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
2364                              *what_next == ValueObject::eDereference &&
2365                              pointee_clang_type_info.Test(ClangASTContext::eTypeIsScalar))
2366                     {
2367                         Error error;
2368                         root = root->Dereference(error);
2369                         if (error.Fail() || !root.get())
2370                         {
2371                             *first_unparsed = expression_cstr;
2372                             *reason_to_stop = ValueObject::eDereferencingFailed;
2373                             *final_result = ValueObject::eInvalid;
2374                             return 0;
2375                         }
2376                         else
2377                         {
2378                             *what_next = ValueObject::eNothing;
2379                             continue;
2380                         }
2381                     }
2382                     else
2383                     {
2384                         for (int index = index_lower;
2385                              index <= index_higher; index++)
2386                         {
2387                             ValueObjectSP child =
2388                                 root->GetChildAtIndex(index, true);
2389                             list->Append(child);
2390                         }
2391                         *first_unparsed = end+1;
2392                         *reason_to_stop = ValueObject::eRangeOperatorExpanded;
2393                         *final_result = ValueObject::eValueObjectList;
2394                         return index_higher-index_lower+1; // tell me number of items I added to the VOList
2395                     }
2396                 }
2397                 break;
2398             }
2399             default: // some non-[ separator, or something entirely wrong, is in the way
2400             {
2401                 *first_unparsed = expression_cstr;
2402                 *reason_to_stop = ValueObject::eUnexpectedSymbol;
2403                 *final_result = ValueObject::eInvalid;
2404                 return 0;
2405                 break;
2406             }
2407         }
2408     }
2409 }
2410 
2411 void
2412 ValueObject::DumpValueObject
2413 (
2414     Stream &s,
2415     ValueObject *valobj,
2416     const char *root_valobj_name,
2417     uint32_t ptr_depth,
2418     uint32_t curr_depth,
2419     uint32_t max_depth,
2420     bool show_types,
2421     bool show_location,
2422     bool use_objc,
2423     lldb::DynamicValueType use_dynamic,
2424     bool scope_already_checked,
2425     bool flat_output
2426 )
2427 {
2428     if (valobj)
2429     {
2430         bool update_success = valobj->UpdateValueIfNeeded ();
2431 
2432         if (update_success && use_dynamic != lldb::eNoDynamicValues)
2433         {
2434             ValueObject *dynamic_value = valobj->GetDynamicValue(use_dynamic).get();
2435             if (dynamic_value)
2436                 valobj = dynamic_value;
2437         }
2438 
2439         clang_type_t clang_type = valobj->GetClangType();
2440 
2441         const Flags type_flags (ClangASTContext::GetTypeInfo (clang_type, NULL, NULL));
2442         const char *err_cstr = NULL;
2443         const bool has_children = type_flags.Test (ClangASTContext::eTypeHasChildren);
2444         const bool has_value = type_flags.Test (ClangASTContext::eTypeHasValue);
2445 
2446         const bool print_valobj = flat_output == false || has_value;
2447 
2448         if (print_valobj)
2449         {
2450             if (show_location)
2451             {
2452                 s.Printf("%s: ", valobj->GetLocationAsCString());
2453             }
2454 
2455             s.Indent();
2456 
2457             // Always show the type for the top level items.
2458             if (show_types || (curr_depth == 0 && !flat_output))
2459                 s.Printf("(%s) ", valobj->GetTypeName().AsCString("<invalid type>"));
2460 
2461 
2462             if (flat_output)
2463             {
2464                 // If we are showing types, also qualify the C++ base classes
2465                 const bool qualify_cxx_base_classes = show_types;
2466                 valobj->GetExpressionPath(s, qualify_cxx_base_classes);
2467                 s.PutCString(" =");
2468             }
2469             else
2470             {
2471                 const char *name_cstr = root_valobj_name ? root_valobj_name : valobj->GetName().AsCString("");
2472                 s.Printf ("%s =", name_cstr);
2473             }
2474 
2475             if (!scope_already_checked && !valobj->IsInScope())
2476             {
2477                 err_cstr = "out of scope";
2478             }
2479         }
2480 
2481         const char *val_cstr = NULL;
2482         const char *sum_cstr = NULL;
2483         SummaryFormat* entry = valobj->GetSummaryFormat().get();
2484 
2485         if (err_cstr == NULL)
2486         {
2487             val_cstr = valobj->GetValueAsCString();
2488             err_cstr = valobj->GetError().AsCString();
2489         }
2490 
2491         if (err_cstr)
2492         {
2493             s.Printf (" <%s>\n", err_cstr);
2494         }
2495         else
2496         {
2497             const bool is_ref = type_flags.Test (ClangASTContext::eTypeIsReference);
2498             if (print_valobj)
2499             {
2500 
2501                 sum_cstr = valobj->GetSummaryAsCString();
2502 
2503                 // We must calculate this value in realtime because entry might alter this variable's value
2504                 // (e.g. by saying ${var%fmt}) and render precached values useless
2505                 if (val_cstr && (!entry || entry->DoesPrintValue() || !sum_cstr))
2506                     s.Printf(" %s", valobj->GetValueAsCString());
2507 
2508                 if(sum_cstr)
2509                 {
2510                     // for some reason, using %@ (ObjC description) in a summary string, makes
2511                     // us believe we need to reset ourselves, thus invalidating the content of
2512                     // sum_cstr. Thus, IF we had a valid sum_cstr before, but it is now empty
2513                     // let us recalculate it!
2514                     if (sum_cstr[0] == '\0')
2515                         s.Printf(" %s", valobj->GetSummaryAsCString());
2516                     else
2517                         s.Printf(" %s", sum_cstr);
2518                 }
2519 
2520                 if (use_objc)
2521                 {
2522                     const char *object_desc = valobj->GetObjectDescription();
2523                     if (object_desc)
2524                         s.Printf(" %s\n", object_desc);
2525                     else
2526                         s.Printf (" [no Objective-C description available]\n");
2527                     return;
2528                 }
2529             }
2530 
2531             if (curr_depth < max_depth)
2532             {
2533                 // We will show children for all concrete types. We won't show
2534                 // pointer contents unless a pointer depth has been specified.
2535                 // We won't reference contents unless the reference is the
2536                 // root object (depth of zero).
2537                 bool print_children = true;
2538 
2539                 // Use a new temporary pointer depth in case we override the
2540                 // current pointer depth below...
2541                 uint32_t curr_ptr_depth = ptr_depth;
2542 
2543                 const bool is_ptr = type_flags.Test (ClangASTContext::eTypeIsPointer);
2544                 if (is_ptr || is_ref)
2545                 {
2546                     // We have a pointer or reference whose value is an address.
2547                     // Make sure that address is not NULL
2548                     AddressType ptr_address_type;
2549                     if (valobj->GetPointerValue (ptr_address_type, true) == 0)
2550                         print_children = false;
2551 
2552                     else if (is_ref && curr_depth == 0)
2553                     {
2554                         // If this is the root object (depth is zero) that we are showing
2555                         // and it is a reference, and no pointer depth has been supplied
2556                         // print out what it references. Don't do this at deeper depths
2557                         // otherwise we can end up with infinite recursion...
2558                         curr_ptr_depth = 1;
2559                     }
2560 
2561                     if (curr_ptr_depth == 0)
2562                         print_children = false;
2563                 }
2564 
2565                 if (print_children && (!entry || entry->DoesPrintChildren() || !sum_cstr))
2566                 {
2567                     const uint32_t num_children = valobj->GetNumChildren();
2568                     if (num_children)
2569                     {
2570                         if (flat_output)
2571                         {
2572                             if (print_valobj)
2573                                 s.EOL();
2574                         }
2575                         else
2576                         {
2577                             if (print_valobj)
2578                                 s.PutCString(is_ref ? ": {\n" : " {\n");
2579                             s.IndentMore();
2580                         }
2581 
2582                         for (uint32_t idx=0; idx<num_children; ++idx)
2583                         {
2584                             ValueObjectSP child_sp(valobj->GetChildAtIndex(idx, true));
2585                             if (child_sp.get())
2586                             {
2587                                 DumpValueObject (s,
2588                                                  child_sp.get(),
2589                                                  NULL,
2590                                                  (is_ptr || is_ref) ? curr_ptr_depth - 1 : curr_ptr_depth,
2591                                                  curr_depth + 1,
2592                                                  max_depth,
2593                                                  show_types,
2594                                                  show_location,
2595                                                  false,
2596                                                  use_dynamic,
2597                                                  true,
2598                                                  flat_output);
2599                             }
2600                         }
2601 
2602                         if (!flat_output)
2603                         {
2604                             s.IndentLess();
2605                             s.Indent("}\n");
2606                         }
2607                     }
2608                     else if (has_children)
2609                     {
2610                         // Aggregate, no children...
2611                         if (print_valobj)
2612                             s.PutCString(" {}\n");
2613                     }
2614                     else
2615                     {
2616                         if (print_valobj)
2617                             s.EOL();
2618                     }
2619 
2620                 }
2621                 else
2622                 {
2623                     s.EOL();
2624                 }
2625             }
2626             else
2627             {
2628                 if (has_children && print_valobj)
2629                 {
2630                     s.PutCString("{...}\n");
2631                 }
2632             }
2633         }
2634     }
2635 }
2636 
2637 
2638 ValueObjectSP
2639 ValueObject::CreateConstantValue (const ConstString &name)
2640 {
2641     ValueObjectSP valobj_sp;
2642 
2643     if (UpdateValueIfNeeded() && m_error.Success())
2644     {
2645         ExecutionContextScope *exe_scope = GetExecutionContextScope();
2646         if (exe_scope)
2647         {
2648             ExecutionContext exe_ctx;
2649             exe_scope->CalculateExecutionContext(exe_ctx);
2650 
2651             clang::ASTContext *ast = GetClangAST ();
2652 
2653             DataExtractor data;
2654             data.SetByteOrder (m_data.GetByteOrder());
2655             data.SetAddressByteSize(m_data.GetAddressByteSize());
2656 
2657             m_error = m_value.GetValueAsData (&exe_ctx, ast, data, 0, GetModule());
2658 
2659             valobj_sp = ValueObjectConstResult::Create (exe_scope,
2660                                                         ast,
2661                                                         GetClangType(),
2662                                                         name,
2663                                                         data);
2664         }
2665     }
2666 
2667     if (!valobj_sp)
2668     {
2669         valobj_sp = ValueObjectConstResult::Create (NULL, m_error);
2670     }
2671     return valobj_sp;
2672 }
2673 
2674 lldb::ValueObjectSP
2675 ValueObject::Dereference (Error &error)
2676 {
2677     if (m_deref_valobj)
2678         return m_deref_valobj->GetSP();
2679 
2680     const bool is_pointer_type = IsPointerType();
2681     if (is_pointer_type)
2682     {
2683         bool omit_empty_base_classes = true;
2684         bool ignore_array_bounds = false;
2685 
2686         std::string child_name_str;
2687         uint32_t child_byte_size = 0;
2688         int32_t child_byte_offset = 0;
2689         uint32_t child_bitfield_bit_size = 0;
2690         uint32_t child_bitfield_bit_offset = 0;
2691         bool child_is_base_class = false;
2692         bool child_is_deref_of_parent = false;
2693         const bool transparent_pointers = false;
2694         clang::ASTContext *clang_ast = GetClangAST();
2695         clang_type_t clang_type = GetClangType();
2696         clang_type_t child_clang_type;
2697 
2698         ExecutionContext exe_ctx;
2699         GetExecutionContextScope()->CalculateExecutionContext (exe_ctx);
2700 
2701         child_clang_type = ClangASTContext::GetChildClangTypeAtIndex (&exe_ctx,
2702                                                                       clang_ast,
2703                                                                       GetName().GetCString(),
2704                                                                       clang_type,
2705                                                                       0,
2706                                                                       transparent_pointers,
2707                                                                       omit_empty_base_classes,
2708                                                                       ignore_array_bounds,
2709                                                                       child_name_str,
2710                                                                       child_byte_size,
2711                                                                       child_byte_offset,
2712                                                                       child_bitfield_bit_size,
2713                                                                       child_bitfield_bit_offset,
2714                                                                       child_is_base_class,
2715                                                                       child_is_deref_of_parent);
2716         if (child_clang_type && child_byte_size)
2717         {
2718             ConstString child_name;
2719             if (!child_name_str.empty())
2720                 child_name.SetCString (child_name_str.c_str());
2721 
2722             m_deref_valobj = new ValueObjectChild (*this,
2723                                                    clang_ast,
2724                                                    child_clang_type,
2725                                                    child_name,
2726                                                    child_byte_size,
2727                                                    child_byte_offset,
2728                                                    child_bitfield_bit_size,
2729                                                    child_bitfield_bit_offset,
2730                                                    child_is_base_class,
2731                                                    child_is_deref_of_parent);
2732         }
2733     }
2734 
2735     if (m_deref_valobj)
2736     {
2737         error.Clear();
2738         return m_deref_valobj->GetSP();
2739     }
2740     else
2741     {
2742         StreamString strm;
2743         GetExpressionPath(strm, true);
2744 
2745         if (is_pointer_type)
2746             error.SetErrorStringWithFormat("dereference failed: (%s) %s", GetTypeName().AsCString("<invalid type>"), strm.GetString().c_str());
2747         else
2748             error.SetErrorStringWithFormat("not a pointer type: (%s) %s", GetTypeName().AsCString("<invalid type>"), strm.GetString().c_str());
2749         return ValueObjectSP();
2750     }
2751 }
2752 
2753 lldb::ValueObjectSP
2754 ValueObject::AddressOf (Error &error)
2755 {
2756     if (m_addr_of_valobj_sp)
2757         return m_addr_of_valobj_sp;
2758 
2759     AddressType address_type = eAddressTypeInvalid;
2760     const bool scalar_is_load_address = false;
2761     lldb::addr_t addr = GetAddressOf (address_type, scalar_is_load_address);
2762     error.Clear();
2763     if (addr != LLDB_INVALID_ADDRESS)
2764     {
2765         switch (address_type)
2766         {
2767         default:
2768         case eAddressTypeInvalid:
2769             {
2770                 StreamString expr_path_strm;
2771                 GetExpressionPath(expr_path_strm, true);
2772                 error.SetErrorStringWithFormat("'%s' is not in memory", expr_path_strm.GetString().c_str());
2773             }
2774             break;
2775 
2776         case eAddressTypeFile:
2777         case eAddressTypeLoad:
2778         case eAddressTypeHost:
2779             {
2780                 clang::ASTContext *ast = GetClangAST();
2781                 clang_type_t clang_type = GetClangType();
2782                 if (ast && clang_type)
2783                 {
2784                     std::string name (1, '&');
2785                     name.append (m_name.AsCString(""));
2786                     m_addr_of_valobj_sp = ValueObjectConstResult::Create (GetExecutionContextScope(),
2787                                                                           ast,
2788                                                                           ClangASTContext::CreatePointerType (ast, clang_type),
2789                                                                           ConstString (name.c_str()),
2790                                                                           addr,
2791                                                                           eAddressTypeInvalid,
2792                                                                           m_data.GetAddressByteSize());
2793                 }
2794             }
2795             break;
2796         }
2797     }
2798     return m_addr_of_valobj_sp;
2799 }
2800 
2801 
2802 lldb::ValueObjectSP
2803 ValueObject::CastPointerType (const char *name, ClangASTType &clang_ast_type)
2804 {
2805     lldb::ValueObjectSP valobj_sp;
2806     AddressType address_type;
2807     const bool scalar_is_load_address = true;
2808     lldb::addr_t ptr_value = GetPointerValue (address_type, scalar_is_load_address);
2809 
2810     if (ptr_value != LLDB_INVALID_ADDRESS)
2811     {
2812         Address ptr_addr (NULL, ptr_value);
2813 
2814         valobj_sp = ValueObjectMemory::Create (GetExecutionContextScope(),
2815                                                name,
2816                                                ptr_addr,
2817                                                clang_ast_type);
2818     }
2819     return valobj_sp;
2820 }
2821 
2822 lldb::ValueObjectSP
2823 ValueObject::CastPointerType (const char *name, TypeSP &type_sp)
2824 {
2825     lldb::ValueObjectSP valobj_sp;
2826     AddressType address_type;
2827     const bool scalar_is_load_address = true;
2828     lldb::addr_t ptr_value = GetPointerValue (address_type, scalar_is_load_address);
2829 
2830     if (ptr_value != LLDB_INVALID_ADDRESS)
2831     {
2832         Address ptr_addr (NULL, ptr_value);
2833 
2834         valobj_sp = ValueObjectMemory::Create (GetExecutionContextScope(),
2835                                                name,
2836                                                ptr_addr,
2837                                                type_sp);
2838     }
2839     return valobj_sp;
2840 }
2841 
2842 
2843 ValueObject::EvaluationPoint::EvaluationPoint () :
2844     m_thread_id (LLDB_INVALID_UID),
2845     m_stop_id (0)
2846 {
2847 }
2848 
2849 ValueObject::EvaluationPoint::EvaluationPoint (ExecutionContextScope *exe_scope, bool use_selected):
2850     m_needs_update (true),
2851     m_first_update (true),
2852     m_thread_id (LLDB_INVALID_THREAD_ID),
2853     m_stop_id (0)
2854 
2855 {
2856     ExecutionContext exe_ctx;
2857     ExecutionContextScope *computed_exe_scope = exe_scope;  // If use_selected is true, we may find a better scope,
2858                                                             // and if so we want to cache that not the original.
2859     if (exe_scope)
2860         exe_scope->CalculateExecutionContext(exe_ctx);
2861     if (exe_ctx.target != NULL)
2862     {
2863         m_target_sp = exe_ctx.target->GetSP();
2864 
2865         if (exe_ctx.process == NULL)
2866             m_process_sp = exe_ctx.target->GetProcessSP();
2867         else
2868             m_process_sp = exe_ctx.process->GetSP();
2869 
2870         if (m_process_sp != NULL)
2871         {
2872             m_stop_id = m_process_sp->GetStopID();
2873             Thread *thread = NULL;
2874 
2875             if (exe_ctx.thread == NULL)
2876             {
2877                 if (use_selected)
2878                 {
2879                     thread = m_process_sp->GetThreadList().GetSelectedThread().get();
2880                     if (thread)
2881                         computed_exe_scope = thread;
2882                 }
2883             }
2884             else
2885                 thread = exe_ctx.thread;
2886 
2887             if (thread != NULL)
2888             {
2889                 m_thread_id = thread->GetIndexID();
2890                 if (exe_ctx.frame == NULL)
2891                 {
2892                     if (use_selected)
2893                     {
2894                         StackFrame *frame = exe_ctx.thread->GetSelectedFrame().get();
2895                         if (frame)
2896                         {
2897                             m_stack_id = frame->GetStackID();
2898                             computed_exe_scope = frame;
2899                         }
2900                     }
2901                 }
2902                 else
2903                     m_stack_id = exe_ctx.frame->GetStackID();
2904             }
2905         }
2906     }
2907     m_exe_scope = computed_exe_scope;
2908 }
2909 
2910 ValueObject::EvaluationPoint::EvaluationPoint (const ValueObject::EvaluationPoint &rhs) :
2911     m_exe_scope (rhs.m_exe_scope),
2912     m_needs_update(true),
2913     m_first_update(true),
2914     m_target_sp (rhs.m_target_sp),
2915     m_process_sp (rhs.m_process_sp),
2916     m_thread_id (rhs.m_thread_id),
2917     m_stack_id (rhs.m_stack_id),
2918     m_stop_id (0)
2919 {
2920 }
2921 
2922 ValueObject::EvaluationPoint::~EvaluationPoint ()
2923 {
2924 }
2925 
2926 ExecutionContextScope *
2927 ValueObject::EvaluationPoint::GetExecutionContextScope ()
2928 {
2929     // We have to update before giving out the scope, or we could be handing out stale pointers.
2930     SyncWithProcessState();
2931 
2932     return m_exe_scope;
2933 }
2934 
2935 // This function checks the EvaluationPoint against the current process state.  If the current
2936 // state matches the evaluation point, or the evaluation point is already invalid, then we return
2937 // false, meaning "no change".  If the current state is different, we update our state, and return
2938 // true meaning "yes, change".  If we did see a change, we also set m_needs_update to true, so
2939 // future calls to NeedsUpdate will return true.
2940 
2941 bool
2942 ValueObject::EvaluationPoint::SyncWithProcessState()
2943 {
2944     // If we're already invalid, we don't need to do anything, and nothing has changed:
2945     if (m_stop_id == LLDB_INVALID_UID)
2946     {
2947         // Can't update with an invalid state.
2948         m_needs_update = false;
2949         return false;
2950     }
2951 
2952     // If we don't have a process nothing can change.
2953     if (!m_process_sp)
2954         return false;
2955 
2956     // If our stop id is the current stop ID, nothing has changed:
2957     uint32_t cur_stop_id = m_process_sp->GetStopID();
2958     if (m_stop_id == cur_stop_id)
2959         return false;
2960 
2961     // If the current stop id is 0, either we haven't run yet, or the process state has been cleared.
2962     // In either case, we aren't going to be able to sync with the process state.
2963     if (cur_stop_id == 0)
2964         return false;
2965 
2966     m_stop_id = cur_stop_id;
2967     m_needs_update = true;
2968     m_exe_scope = m_process_sp.get();
2969 
2970     // Something has changed, so we will return true.  Now make sure the thread & frame still exist, and if either
2971     // doesn't, mark ourselves as invalid.
2972 
2973     if (m_thread_id != LLDB_INVALID_THREAD_ID)
2974     {
2975         Thread *our_thread = m_process_sp->GetThreadList().FindThreadByIndexID (m_thread_id).get();
2976         if (our_thread == NULL)
2977         {
2978             SetInvalid();
2979         }
2980         else
2981         {
2982             m_exe_scope = our_thread;
2983 
2984             if (m_stack_id.IsValid())
2985             {
2986                 StackFrame *our_frame = our_thread->GetFrameWithStackID (m_stack_id).get();
2987                 if (our_frame == NULL)
2988                     SetInvalid();
2989                 else
2990                     m_exe_scope = our_frame;
2991             }
2992         }
2993     }
2994     return true;
2995 }
2996 
2997 void
2998 ValueObject::EvaluationPoint::SetUpdated ()
2999 {
3000     m_first_update = false;
3001     m_needs_update = false;
3002     if (m_process_sp)
3003         m_stop_id = m_process_sp->GetStopID();
3004 }
3005 
3006 
3007 bool
3008 ValueObject::EvaluationPoint::SetContext (ExecutionContextScope *exe_scope)
3009 {
3010     if (!IsValid())
3011         return false;
3012 
3013     bool needs_update = false;
3014     m_exe_scope = NULL;
3015 
3016     // The target has to be non-null, and the
3017     Target *target = exe_scope->CalculateTarget();
3018     if (target != NULL)
3019     {
3020         Target *old_target = m_target_sp.get();
3021         assert (target == old_target);
3022         Process *process = exe_scope->CalculateProcess();
3023         if (process != NULL)
3024         {
3025             // FOR NOW - assume you can't update variable objects across process boundaries.
3026             Process *old_process = m_process_sp.get();
3027             assert (process == old_process);
3028 
3029             lldb::user_id_t stop_id = process->GetStopID();
3030             if (stop_id != m_stop_id)
3031             {
3032                 needs_update = true;
3033                 m_stop_id = stop_id;
3034             }
3035             // See if we're switching the thread or stack context.  If no thread is given, this is
3036             // being evaluated in a global context.
3037             Thread *thread = exe_scope->CalculateThread();
3038             if (thread != NULL)
3039             {
3040                 lldb::user_id_t new_thread_index = thread->GetIndexID();
3041                 if (new_thread_index != m_thread_id)
3042                 {
3043                     needs_update = true;
3044                     m_thread_id = new_thread_index;
3045                     m_stack_id.Clear();
3046                 }
3047 
3048                 StackFrame *new_frame = exe_scope->CalculateStackFrame();
3049                 if (new_frame != NULL)
3050                 {
3051                     if (new_frame->GetStackID() != m_stack_id)
3052                     {
3053                         needs_update = true;
3054                         m_stack_id = new_frame->GetStackID();
3055                     }
3056                 }
3057                 else
3058                 {
3059                     m_stack_id.Clear();
3060                     needs_update = true;
3061                 }
3062             }
3063             else
3064             {
3065                 // If this had been given a thread, and now there is none, we should update.
3066                 // Otherwise we don't have to do anything.
3067                 if (m_thread_id != LLDB_INVALID_UID)
3068                 {
3069                     m_thread_id = LLDB_INVALID_UID;
3070                     m_stack_id.Clear();
3071                     needs_update = true;
3072                 }
3073             }
3074         }
3075         else
3076         {
3077             // If there is no process, then we don't need to update anything.
3078             // But if we're switching from having a process to not, we should try to update.
3079             if (m_process_sp.get() != NULL)
3080             {
3081                 needs_update = true;
3082                 m_process_sp.reset();
3083                 m_thread_id = LLDB_INVALID_UID;
3084                 m_stack_id.Clear();
3085             }
3086         }
3087     }
3088     else
3089     {
3090         // If there's no target, nothing can change so we don't need to update anything.
3091         // But if we're switching from having a target to not, we should try to update.
3092         if (m_target_sp.get() != NULL)
3093         {
3094             needs_update = true;
3095             m_target_sp.reset();
3096             m_process_sp.reset();
3097             m_thread_id = LLDB_INVALID_UID;
3098             m_stack_id.Clear();
3099         }
3100     }
3101     if (!m_needs_update)
3102         m_needs_update = needs_update;
3103 
3104     return needs_update;
3105 }
3106