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