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