Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-05-10 08:42:59

0001 //===-- ValueObject.h -------------------------------------------*- C++ -*-===//
0002 //
0003 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
0004 // See https://llvm.org/LICENSE.txt for license information.
0005 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
0006 //
0007 //===----------------------------------------------------------------------===//
0008 
0009 #ifndef LLDB_VALUEOBJECT_VALUEOBJECT_H
0010 #define LLDB_VALUEOBJECT_VALUEOBJECT_H
0011 
0012 #include "lldb/Core/Value.h"
0013 #include "lldb/Symbol/CompilerType.h"
0014 #include "lldb/Symbol/Type.h"
0015 #include "lldb/Target/ExecutionContext.h"
0016 #include "lldb/Target/Process.h"
0017 #include "lldb/Utility/ConstString.h"
0018 #include "lldb/Utility/DataExtractor.h"
0019 #include "lldb/Utility/SharedCluster.h"
0020 #include "lldb/Utility/Status.h"
0021 #include "lldb/Utility/UserID.h"
0022 #include "lldb/lldb-defines.h"
0023 #include "lldb/lldb-enumerations.h"
0024 #include "lldb/lldb-forward.h"
0025 #include "lldb/lldb-private-enumerations.h"
0026 #include "lldb/lldb-types.h"
0027 
0028 #include "llvm/ADT/ArrayRef.h"
0029 #include "llvm/ADT/SmallVector.h"
0030 #include "llvm/ADT/StringRef.h"
0031 
0032 #include <functional>
0033 #include <initializer_list>
0034 #include <map>
0035 #include <mutex>
0036 #include <optional>
0037 #include <string>
0038 #include <utility>
0039 
0040 #include <cstddef>
0041 #include <cstdint>
0042 
0043 namespace lldb_private {
0044 class Declaration;
0045 class DumpValueObjectOptions;
0046 class EvaluateExpressionOptions;
0047 class ExecutionContextScope;
0048 class Log;
0049 class Scalar;
0050 class Stream;
0051 class SymbolContextScope;
0052 class TypeFormatImpl;
0053 class TypeSummaryImpl;
0054 class TypeSummaryOptions;
0055 
0056 /// ValueObject:
0057 ///
0058 /// This abstract class provides an interface to a particular value, be it a
0059 /// register, a local or global variable,
0060 /// that is evaluated in some particular scope.  The ValueObject also has the
0061 /// capability of being the "child" of
0062 /// some other variable object, and in turn of having children.
0063 /// If a ValueObject is a root variable object - having no parent - then it must
0064 /// be constructed with respect to some
0065 /// particular ExecutionContextScope.  If it is a child, it inherits the
0066 /// ExecutionContextScope from its parent.
0067 /// The ValueObject will update itself if necessary before fetching its value,
0068 /// summary, object description, etc.
0069 /// But it will always update itself in the ExecutionContextScope with which it
0070 /// was originally created.
0071 
0072 /// A brief note on life cycle management for ValueObjects.  This is a little
0073 /// tricky because a ValueObject can contain
0074 /// various other ValueObjects - the Dynamic Value, its children, the
0075 /// dereference value, etc.  Any one of these can be
0076 /// handed out as a shared pointer, but for that contained value object to be
0077 /// valid, the root object and potentially other
0078 /// of the value objects need to stay around.
0079 /// We solve this problem by handing out shared pointers to the Value Object and
0080 /// any of its dependents using a shared
0081 /// ClusterManager.  This treats each shared pointer handed out for the entire
0082 /// cluster as a reference to the whole
0083 /// cluster.  The whole cluster will stay around until the last reference is
0084 /// released.
0085 ///
0086 /// The ValueObject mostly handle this automatically, if a value object is made
0087 /// with a Parent ValueObject, then it adds
0088 /// itself to the ClusterManager of the parent.
0089 
0090 /// It does mean that external to the ValueObjects we should only ever make
0091 /// available ValueObjectSP's, never ValueObjects
0092 /// or pointers to them.  So all the "Root level" ValueObject derived
0093 /// constructors should be private, and
0094 /// should implement a Create function that new's up object and returns a Shared
0095 /// Pointer that it gets from the GetSP() method.
0096 ///
0097 /// However, if you are making an derived ValueObject that will be contained in
0098 /// a parent value object, you should just
0099 /// hold onto a pointer to it internally, and by virtue of passing the parent
0100 /// ValueObject into its constructor, it will
0101 /// be added to the ClusterManager for the parent.  Then if you ever hand out a
0102 /// Shared Pointer to the contained ValueObject,
0103 /// just do so by calling GetSP() on the contained object.
0104 
0105 class ValueObject {
0106 public:
0107   enum GetExpressionPathFormat {
0108     eGetExpressionPathFormatDereferencePointers = 1,
0109     eGetExpressionPathFormatHonorPointers
0110   };
0111 
0112   enum ValueObjectRepresentationStyle {
0113     eValueObjectRepresentationStyleValue = 1,
0114     eValueObjectRepresentationStyleSummary,
0115     eValueObjectRepresentationStyleLanguageSpecific,
0116     eValueObjectRepresentationStyleLocation,
0117     eValueObjectRepresentationStyleChildrenCount,
0118     eValueObjectRepresentationStyleType,
0119     eValueObjectRepresentationStyleName,
0120     eValueObjectRepresentationStyleExpressionPath
0121   };
0122 
0123   enum ExpressionPathScanEndReason {
0124     /// Out of data to parse.
0125     eExpressionPathScanEndReasonEndOfString = 1,
0126     /// Child element not found.
0127     eExpressionPathScanEndReasonNoSuchChild,
0128     /// (Synthetic) child  element not found.
0129     eExpressionPathScanEndReasonNoSuchSyntheticChild,
0130     /// [] only allowed for arrays.
0131     eExpressionPathScanEndReasonEmptyRangeNotAllowed,
0132     /// . used when -> should be used.
0133     eExpressionPathScanEndReasonDotInsteadOfArrow,
0134     /// -> used when . should be used.
0135     eExpressionPathScanEndReasonArrowInsteadOfDot,
0136     /// ObjC ivar expansion not allowed.
0137     eExpressionPathScanEndReasonFragileIVarNotAllowed,
0138     /// [] not allowed by options.
0139     eExpressionPathScanEndReasonRangeOperatorNotAllowed,
0140     /// [] not valid on objects  other than scalars, pointers or arrays.
0141     eExpressionPathScanEndReasonRangeOperatorInvalid,
0142     /// [] is good for arrays,  but I cannot parse it.
0143     eExpressionPathScanEndReasonArrayRangeOperatorMet,
0144     /// [] is good for bitfields, but I cannot parse after it.
0145     eExpressionPathScanEndReasonBitfieldRangeOperatorMet,
0146     /// Something is malformed in he expression.
0147     eExpressionPathScanEndReasonUnexpectedSymbol,
0148     /// Impossible to apply &  operator.
0149     eExpressionPathScanEndReasonTakingAddressFailed,
0150     /// Impossible to apply *  operator.
0151     eExpressionPathScanEndReasonDereferencingFailed,
0152     /// [] was expanded into a  VOList.
0153     eExpressionPathScanEndReasonRangeOperatorExpanded,
0154     /// getting the synthetic children failed.
0155     eExpressionPathScanEndReasonSyntheticValueMissing,
0156     eExpressionPathScanEndReasonUnknown = 0xFFFF
0157   };
0158 
0159   enum ExpressionPathEndResultType {
0160     /// Anything but...
0161     eExpressionPathEndResultTypePlain = 1,
0162     /// A bitfield.
0163     eExpressionPathEndResultTypeBitfield,
0164     /// A range [low-high].
0165     eExpressionPathEndResultTypeBoundedRange,
0166     /// A range [].
0167     eExpressionPathEndResultTypeUnboundedRange,
0168     /// Several items in a VOList.
0169     eExpressionPathEndResultTypeValueObjectList,
0170     eExpressionPathEndResultTypeInvalid = 0xFFFF
0171   };
0172 
0173   enum ExpressionPathAftermath {
0174     /// Just return it.
0175     eExpressionPathAftermathNothing = 1,
0176     /// Dereference the target.
0177     eExpressionPathAftermathDereference,
0178     /// Take target's address.
0179     eExpressionPathAftermathTakeAddress
0180   };
0181 
0182   enum ClearUserVisibleDataItems {
0183     eClearUserVisibleDataItemsNothing = 1u << 0,
0184     eClearUserVisibleDataItemsValue = 1u << 1,
0185     eClearUserVisibleDataItemsSummary = 1u << 2,
0186     eClearUserVisibleDataItemsLocation = 1u << 3,
0187     eClearUserVisibleDataItemsDescription = 1u << 4,
0188     eClearUserVisibleDataItemsSyntheticChildren = 1u << 5,
0189     eClearUserVisibleDataItemsAllStrings =
0190         eClearUserVisibleDataItemsValue | eClearUserVisibleDataItemsSummary |
0191         eClearUserVisibleDataItemsLocation |
0192         eClearUserVisibleDataItemsDescription,
0193     eClearUserVisibleDataItemsAll = 0xFFFF
0194   };
0195 
0196   struct GetValueForExpressionPathOptions {
0197     enum class SyntheticChildrenTraversal {
0198       None,
0199       ToSynthetic,
0200       FromSynthetic,
0201       Both
0202     };
0203 
0204     bool m_check_dot_vs_arrow_syntax;
0205     bool m_no_fragile_ivar;
0206     bool m_allow_bitfields_syntax;
0207     SyntheticChildrenTraversal m_synthetic_children_traversal;
0208 
0209     GetValueForExpressionPathOptions(
0210         bool dot = false, bool no_ivar = false, bool bitfield = true,
0211         SyntheticChildrenTraversal synth_traverse =
0212             SyntheticChildrenTraversal::ToSynthetic)
0213         : m_check_dot_vs_arrow_syntax(dot), m_no_fragile_ivar(no_ivar),
0214           m_allow_bitfields_syntax(bitfield),
0215           m_synthetic_children_traversal(synth_traverse) {}
0216 
0217     GetValueForExpressionPathOptions &DoCheckDotVsArrowSyntax() {
0218       m_check_dot_vs_arrow_syntax = true;
0219       return *this;
0220     }
0221 
0222     GetValueForExpressionPathOptions &DontCheckDotVsArrowSyntax() {
0223       m_check_dot_vs_arrow_syntax = false;
0224       return *this;
0225     }
0226 
0227     GetValueForExpressionPathOptions &DoAllowFragileIVar() {
0228       m_no_fragile_ivar = false;
0229       return *this;
0230     }
0231 
0232     GetValueForExpressionPathOptions &DontAllowFragileIVar() {
0233       m_no_fragile_ivar = true;
0234       return *this;
0235     }
0236 
0237     GetValueForExpressionPathOptions &DoAllowBitfieldSyntax() {
0238       m_allow_bitfields_syntax = true;
0239       return *this;
0240     }
0241 
0242     GetValueForExpressionPathOptions &DontAllowBitfieldSyntax() {
0243       m_allow_bitfields_syntax = false;
0244       return *this;
0245     }
0246 
0247     GetValueForExpressionPathOptions &
0248     SetSyntheticChildrenTraversal(SyntheticChildrenTraversal traverse) {
0249       m_synthetic_children_traversal = traverse;
0250       return *this;
0251     }
0252 
0253     static const GetValueForExpressionPathOptions DefaultOptions() {
0254       static GetValueForExpressionPathOptions g_default_options;
0255 
0256       return g_default_options;
0257     }
0258   };
0259 
0260   class EvaluationPoint {
0261   public:
0262     EvaluationPoint();
0263 
0264     EvaluationPoint(ExecutionContextScope *exe_scope,
0265                     bool use_selected = false);
0266 
0267     EvaluationPoint(const EvaluationPoint &rhs);
0268 
0269     ~EvaluationPoint();
0270 
0271     const ExecutionContextRef &GetExecutionContextRef() const {
0272       return m_exe_ctx_ref;
0273     }
0274 
0275     void SetIsConstant() {
0276       SetUpdated();
0277       m_mod_id.SetInvalid();
0278     }
0279 
0280     bool IsConstant() const { return !m_mod_id.IsValid(); }
0281 
0282     ProcessModID GetModID() const { return m_mod_id; }
0283 
0284     void SetUpdateID(ProcessModID new_id) { m_mod_id = new_id; }
0285 
0286     void SetNeedsUpdate() { m_needs_update = true; }
0287 
0288     void SetUpdated();
0289 
0290     bool NeedsUpdating(bool accept_invalid_exe_ctx) {
0291       SyncWithProcessState(accept_invalid_exe_ctx);
0292       return m_needs_update;
0293     }
0294 
0295     bool IsValid() {
0296       const bool accept_invalid_exe_ctx = false;
0297       if (!m_mod_id.IsValid())
0298         return false;
0299       else if (SyncWithProcessState(accept_invalid_exe_ctx)) {
0300         if (!m_mod_id.IsValid())
0301           return false;
0302       }
0303       return true;
0304     }
0305 
0306     void SetInvalid() {
0307       // Use the stop id to mark us as invalid, leave the thread id and the
0308       // stack id around for logging and history purposes.
0309       m_mod_id.SetInvalid();
0310 
0311       // Can't update an invalid state.
0312       m_needs_update = false;
0313     }
0314 
0315   private:
0316     bool SyncWithProcessState(bool accept_invalid_exe_ctx);
0317 
0318     ProcessModID m_mod_id; // This is the stop id when this ValueObject was last
0319                            // evaluated.
0320     ExecutionContextRef m_exe_ctx_ref;
0321     bool m_needs_update = true;
0322   };
0323 
0324   virtual ~ValueObject();
0325 
0326   const EvaluationPoint &GetUpdatePoint() const { return m_update_point; }
0327 
0328   EvaluationPoint &GetUpdatePoint() { return m_update_point; }
0329 
0330   const ExecutionContextRef &GetExecutionContextRef() const {
0331     return m_update_point.GetExecutionContextRef();
0332   }
0333 
0334   lldb::TargetSP GetTargetSP() const {
0335     return m_update_point.GetExecutionContextRef().GetTargetSP();
0336   }
0337 
0338   lldb::ProcessSP GetProcessSP() const {
0339     return m_update_point.GetExecutionContextRef().GetProcessSP();
0340   }
0341 
0342   lldb::ThreadSP GetThreadSP() const {
0343     return m_update_point.GetExecutionContextRef().GetThreadSP();
0344   }
0345 
0346   lldb::StackFrameSP GetFrameSP() const {
0347     return m_update_point.GetExecutionContextRef().GetFrameSP();
0348   }
0349 
0350   void SetNeedsUpdate();
0351 
0352   CompilerType GetCompilerType() { return MaybeCalculateCompleteType(); }
0353 
0354   // this vends a TypeImpl that is useful at the SB API layer
0355   virtual TypeImpl GetTypeImpl() { return TypeImpl(GetCompilerType()); }
0356 
0357   virtual bool CanProvideValue();
0358 
0359   // Subclasses must implement the functions below.
0360   virtual std::optional<uint64_t> GetByteSize() = 0;
0361 
0362   virtual lldb::ValueType GetValueType() const = 0;
0363 
0364   // Subclasses can implement the functions below.
0365   virtual ConstString GetTypeName() { return GetCompilerType().GetTypeName(); }
0366 
0367   virtual ConstString GetDisplayTypeName() { return GetTypeName(); }
0368 
0369   virtual ConstString GetQualifiedTypeName() {
0370     return GetCompilerType().GetTypeName();
0371   }
0372 
0373   lldb::LanguageType GetObjectRuntimeLanguage() {
0374     return GetCompilerType().GetMinimumLanguage();
0375   }
0376 
0377   uint32_t
0378   GetTypeInfo(CompilerType *pointee_or_element_compiler_type = nullptr) {
0379     return GetCompilerType().GetTypeInfo(pointee_or_element_compiler_type);
0380   }
0381 
0382   bool IsPointerType() { return GetCompilerType().IsPointerType(); }
0383 
0384   bool IsArrayType() { return GetCompilerType().IsArrayType(); }
0385 
0386   bool IsScalarType() { return GetCompilerType().IsScalarType(); }
0387 
0388   bool IsPointerOrReferenceType() {
0389     return GetCompilerType().IsPointerOrReferenceType();
0390   }
0391 
0392   bool IsPossibleDynamicType();
0393 
0394   bool IsNilReference();
0395 
0396   bool IsUninitializedReference();
0397 
0398   virtual bool IsBaseClass() { return false; }
0399 
0400   bool IsBaseClass(uint32_t &depth);
0401 
0402   virtual bool IsDereferenceOfParent() { return false; }
0403 
0404   bool IsIntegerType(bool &is_signed) {
0405     return GetCompilerType().IsIntegerType(is_signed);
0406   }
0407 
0408   virtual void GetExpressionPath(
0409       Stream &s,
0410       GetExpressionPathFormat = eGetExpressionPathFormatDereferencePointers);
0411 
0412   lldb::ValueObjectSP GetValueForExpressionPath(
0413       llvm::StringRef expression,
0414       ExpressionPathScanEndReason *reason_to_stop = nullptr,
0415       ExpressionPathEndResultType *final_value_type = nullptr,
0416       const GetValueForExpressionPathOptions &options =
0417           GetValueForExpressionPathOptions::DefaultOptions(),
0418       ExpressionPathAftermath *final_task_on_target = nullptr);
0419 
0420   virtual bool IsInScope() { return true; }
0421 
0422   virtual lldb::offset_t GetByteOffset() { return 0; }
0423 
0424   virtual uint32_t GetBitfieldBitSize() { return 0; }
0425 
0426   virtual uint32_t GetBitfieldBitOffset() { return 0; }
0427 
0428   bool IsBitfield() {
0429     return (GetBitfieldBitSize() != 0) || (GetBitfieldBitOffset() != 0);
0430   }
0431 
0432   virtual const char *GetValueAsCString();
0433 
0434   virtual bool GetValueAsCString(const lldb_private::TypeFormatImpl &format,
0435                                  std::string &destination);
0436 
0437   bool GetValueAsCString(lldb::Format format, std::string &destination);
0438 
0439   virtual uint64_t GetValueAsUnsigned(uint64_t fail_value,
0440                                       bool *success = nullptr);
0441 
0442   virtual int64_t GetValueAsSigned(int64_t fail_value, bool *success = nullptr);
0443 
0444   /// If the current ValueObject is of an appropriate type, convert the
0445   /// value to an APSInt and return that. Otherwise return an error.
0446   llvm::Expected<llvm::APSInt> GetValueAsAPSInt();
0447 
0448   /// If the current ValueObject is of an appropriate type, convert the
0449   /// value to an APFloat and return that. Otherwise return an error.
0450   llvm::Expected<llvm::APFloat> GetValueAsAPFloat();
0451 
0452   /// If the current ValueObject is of an appropriate type, convert the
0453   /// value to a boolean and return that. Otherwise return an error.
0454   llvm::Expected<bool> GetValueAsBool();
0455 
0456   /// Update an existing integer ValueObject with a new integer value. This
0457   /// is only intended to be used with 'temporary' ValueObjects, i.e. ones that
0458   /// are not associated with program variables. It does not update program
0459   /// memory, registers, stack, etc.
0460   void SetValueFromInteger(const llvm::APInt &value, Status &error);
0461 
0462   /// Update an existing integer ValueObject with an integer value created
0463   /// frome 'new_val_sp'.  This is only intended to be used with 'temporary'
0464   /// ValueObjects, i.e. ones that are not associated with program variables.
0465   /// It does not update program  memory, registers, stack, etc.
0466   void SetValueFromInteger(lldb::ValueObjectSP new_val_sp, Status &error);
0467 
0468   virtual bool SetValueFromCString(const char *value_str, Status &error);
0469 
0470   /// Return the module associated with this value object in case the value is
0471   /// from an executable file and might have its data in sections of the file.
0472   /// This can be used for variables.
0473   virtual lldb::ModuleSP GetModule();
0474 
0475   ValueObject *GetRoot();
0476 
0477   /// Given a ValueObject, loop over itself and its parent, and its parent's
0478   /// parent, .. until either the given callback returns false, or you end up at
0479   /// a null pointer
0480   ValueObject *FollowParentChain(std::function<bool(ValueObject *)>);
0481 
0482   virtual bool GetDeclaration(Declaration &decl);
0483 
0484   // The functions below should NOT be modified by subclasses
0485   const Status &GetError();
0486 
0487   ConstString GetName() const { return m_name; }
0488 
0489   /// Returns a unique id for this ValueObject.
0490   lldb::user_id_t GetID() const { return m_id.GetID(); }
0491 
0492   virtual lldb::ValueObjectSP GetChildAtIndex(uint32_t idx,
0493                                               bool can_create = true);
0494 
0495   // The method always creates missing children in the path, if necessary.
0496   lldb::ValueObjectSP GetChildAtNamePath(llvm::ArrayRef<llvm::StringRef> names);
0497 
0498   virtual lldb::ValueObjectSP GetChildMemberWithName(llvm::StringRef name,
0499                                                      bool can_create = true);
0500 
0501   virtual size_t GetIndexOfChildWithName(llvm::StringRef name);
0502 
0503   llvm::Expected<uint32_t> GetNumChildren(uint32_t max = UINT32_MAX);
0504   /// Like \c GetNumChildren but returns 0 on error.  You probably
0505   /// shouldn't be using this function. It exists primarily to ease the
0506   /// transition to more pervasive error handling while not all APIs
0507   /// have been updated.
0508   uint32_t GetNumChildrenIgnoringErrors(uint32_t max = UINT32_MAX);
0509   bool HasChildren() { return GetNumChildrenIgnoringErrors() > 0; }
0510 
0511   const Value &GetValue() const { return m_value; }
0512 
0513   Value &GetValue() { return m_value; }
0514 
0515   virtual bool ResolveValue(Scalar &scalar);
0516 
0517   // return 'false' whenever you set the error, otherwise callers may assume
0518   // true means everything is OK - this will break breakpoint conditions among
0519   // potentially a few others
0520   virtual bool IsLogicalTrue(Status &error);
0521 
0522   virtual const char *GetLocationAsCString() {
0523     return GetLocationAsCStringImpl(m_value, m_data);
0524   }
0525 
0526   const char *
0527   GetSummaryAsCString(lldb::LanguageType lang = lldb::eLanguageTypeUnknown);
0528 
0529   bool
0530   GetSummaryAsCString(TypeSummaryImpl *summary_ptr, std::string &destination,
0531                       lldb::LanguageType lang = lldb::eLanguageTypeUnknown);
0532 
0533   bool GetSummaryAsCString(std::string &destination,
0534                            const TypeSummaryOptions &options);
0535 
0536   bool GetSummaryAsCString(TypeSummaryImpl *summary_ptr,
0537                            std::string &destination,
0538                            const TypeSummaryOptions &options);
0539 
0540   llvm::Expected<std::string> GetObjectDescription();
0541 
0542   bool HasSpecialPrintableRepresentation(
0543       ValueObjectRepresentationStyle val_obj_display,
0544       lldb::Format custom_format);
0545 
0546   enum class PrintableRepresentationSpecialCases : bool {
0547     eDisable = false,
0548     eAllow = true
0549   };
0550 
0551   bool
0552   DumpPrintableRepresentation(Stream &s,
0553                               ValueObjectRepresentationStyle val_obj_display =
0554                                   eValueObjectRepresentationStyleSummary,
0555                               lldb::Format custom_format = lldb::eFormatInvalid,
0556                               PrintableRepresentationSpecialCases special =
0557                                   PrintableRepresentationSpecialCases::eAllow,
0558                               bool do_dump_error = true);
0559   bool GetValueIsValid() const { return m_flags.m_value_is_valid; }
0560 
0561   // If you call this on a newly created ValueObject, it will always return
0562   // false.
0563   bool GetValueDidChange() { return m_flags.m_value_did_change; }
0564 
0565   bool UpdateValueIfNeeded(bool update_format = true);
0566 
0567   bool UpdateFormatsIfNeeded();
0568 
0569   lldb::ValueObjectSP GetSP() { return m_manager->GetSharedPointer(this); }
0570 
0571   /// Change the name of the current ValueObject. Should *not* be used from a
0572   /// synthetic child provider as it would change the name of the non synthetic
0573   /// child as well.
0574   void SetName(ConstString name) { m_name = name; }
0575 
0576   virtual lldb::addr_t GetAddressOf(bool scalar_is_load_address = true,
0577                                     AddressType *address_type = nullptr);
0578 
0579   lldb::addr_t GetPointerValue(AddressType *address_type = nullptr);
0580 
0581   lldb::ValueObjectSP GetSyntheticChild(ConstString key) const;
0582 
0583   lldb::ValueObjectSP GetSyntheticArrayMember(size_t index, bool can_create);
0584 
0585   lldb::ValueObjectSP GetSyntheticBitFieldChild(uint32_t from, uint32_t to,
0586                                                 bool can_create);
0587 
0588   lldb::ValueObjectSP GetSyntheticExpressionPathChild(const char *expression,
0589                                                       bool can_create);
0590 
0591   virtual lldb::ValueObjectSP
0592   GetSyntheticChildAtOffset(uint32_t offset, const CompilerType &type,
0593                             bool can_create,
0594                             ConstString name_const_str = ConstString());
0595 
0596   virtual lldb::ValueObjectSP
0597   GetSyntheticBase(uint32_t offset, const CompilerType &type, bool can_create,
0598                    ConstString name_const_str = ConstString());
0599 
0600   virtual lldb::ValueObjectSP GetDynamicValue(lldb::DynamicValueType valueType);
0601 
0602   lldb::DynamicValueType GetDynamicValueType();
0603 
0604   virtual lldb::ValueObjectSP GetStaticValue() { return GetSP(); }
0605 
0606   virtual lldb::ValueObjectSP GetNonSyntheticValue() { return GetSP(); }
0607 
0608   lldb::ValueObjectSP GetSyntheticValue();
0609 
0610   virtual bool HasSyntheticValue();
0611 
0612   virtual bool IsSynthetic() { return false; }
0613 
0614   lldb::ValueObjectSP
0615   GetQualifiedRepresentationIfAvailable(lldb::DynamicValueType dynValue,
0616                                         bool synthValue);
0617 
0618   virtual lldb::ValueObjectSP CreateConstantValue(ConstString name);
0619 
0620   virtual lldb::ValueObjectSP Dereference(Status &error);
0621 
0622   /// Creates a copy of the ValueObject with a new name and setting the current
0623   /// ValueObject as its parent. It should be used when we want to change the
0624   /// name of a ValueObject without modifying the actual ValueObject itself
0625   /// (e.g. sythetic child provider).
0626   virtual lldb::ValueObjectSP Clone(ConstString new_name);
0627 
0628   virtual lldb::ValueObjectSP AddressOf(Status &error);
0629 
0630   virtual lldb::addr_t GetLiveAddress() { return LLDB_INVALID_ADDRESS; }
0631 
0632   virtual void SetLiveAddress(lldb::addr_t addr = LLDB_INVALID_ADDRESS,
0633                               AddressType address_type = eAddressTypeLoad) {}
0634 
0635   lldb::ValueObjectSP Cast(const CompilerType &compiler_type);
0636 
0637   virtual lldb::ValueObjectSP DoCast(const CompilerType &compiler_type);
0638 
0639   virtual lldb::ValueObjectSP CastPointerType(const char *name,
0640                                               CompilerType &ast_type);
0641 
0642   virtual lldb::ValueObjectSP CastPointerType(const char *name,
0643                                               lldb::TypeSP &type_sp);
0644 
0645   /// Return the target load address associated with this value object.
0646   lldb::addr_t GetLoadAddress();
0647 
0648   /// Take a ValueObject whose type is an inherited class, and cast it to
0649   /// 'type', which should be one of its base classes. 'base_type_indices'
0650   /// contains the indices of direct base classes on the path from the
0651   /// ValueObject's current type to 'type'
0652   llvm::Expected<lldb::ValueObjectSP>
0653   CastDerivedToBaseType(CompilerType type,
0654                         const llvm::ArrayRef<uint32_t> &base_type_indices);
0655 
0656   /// Take a ValueObject whose type is a base class, and cast it to 'type',
0657   /// which should be one of its derived classes. 'base_type_indices'
0658   /// contains the indices of direct base classes on the path from the
0659   /// ValueObject's current type to 'type'
0660   llvm::Expected<lldb::ValueObjectSP> CastBaseToDerivedType(CompilerType type,
0661                                                             uint64_t offset);
0662 
0663   // Take a ValueObject that contains a scalar, enum or pointer type, and
0664   // cast it to a "basic" type (integer, float or boolean).
0665   lldb::ValueObjectSP CastToBasicType(CompilerType type);
0666 
0667   // Take a ValueObject that contain an integer, float or enum, and cast it
0668   // to an enum.
0669   lldb::ValueObjectSP CastToEnumType(CompilerType type);
0670 
0671   /// If this object represents a C++ class with a vtable, return an object
0672   /// that represents the virtual function table. If the object isn't a class
0673   /// with a vtable, return a valid ValueObject with the error set correctly.
0674   lldb::ValueObjectSP GetVTable();
0675   // The backing bits of this value object were updated, clear any descriptive
0676   // string, so we know we have to refetch them.
0677   void ValueUpdated() {
0678     ClearUserVisibleData(eClearUserVisibleDataItemsValue |
0679                          eClearUserVisibleDataItemsSummary |
0680                          eClearUserVisibleDataItemsDescription);
0681   }
0682 
0683   virtual bool IsDynamic() { return false; }
0684 
0685   virtual bool DoesProvideSyntheticValue() { return false; }
0686 
0687   virtual bool IsSyntheticChildrenGenerated() {
0688     return m_flags.m_is_synthetic_children_generated;
0689   }
0690 
0691   virtual void SetSyntheticChildrenGenerated(bool b) {
0692     m_flags.m_is_synthetic_children_generated = b;
0693   }
0694 
0695   virtual SymbolContextScope *GetSymbolContextScope();
0696 
0697   llvm::Error Dump(Stream &s);
0698 
0699   llvm::Error Dump(Stream &s, const DumpValueObjectOptions &options);
0700 
0701   static lldb::ValueObjectSP
0702   CreateValueObjectFromExpression(llvm::StringRef name,
0703                                   llvm::StringRef expression,
0704                                   const ExecutionContext &exe_ctx);
0705 
0706   static lldb::ValueObjectSP
0707   CreateValueObjectFromExpression(llvm::StringRef name,
0708                                   llvm::StringRef expression,
0709                                   const ExecutionContext &exe_ctx,
0710                                   const EvaluateExpressionOptions &options);
0711 
0712   /// Given an address either create a value object containing the value at
0713   /// that address, or create a value object containing the address itself
0714   /// (pointer value), depending on whether the parameter 'do_deref' is true or
0715   /// false.
0716   static lldb::ValueObjectSP
0717   CreateValueObjectFromAddress(llvm::StringRef name, uint64_t address,
0718                                const ExecutionContext &exe_ctx,
0719                                CompilerType type, bool do_deref = true);
0720 
0721   static lldb::ValueObjectSP
0722   CreateValueObjectFromData(llvm::StringRef name, const DataExtractor &data,
0723                             const ExecutionContext &exe_ctx, CompilerType type);
0724 
0725   /// Create a value object containing the given APInt value.
0726   static lldb::ValueObjectSP CreateValueObjectFromAPInt(lldb::TargetSP target,
0727                                                         const llvm::APInt &v,
0728                                                         CompilerType type,
0729                                                         llvm::StringRef name);
0730 
0731   /// Create a value object containing the given APFloat value.
0732   static lldb::ValueObjectSP
0733   CreateValueObjectFromAPFloat(lldb::TargetSP target, const llvm::APFloat &v,
0734                                CompilerType type, llvm::StringRef name);
0735 
0736   /// Create a value object containing the given boolean value.
0737   static lldb::ValueObjectSP CreateValueObjectFromBool(lldb::TargetSP target,
0738                                                        bool value,
0739                                                        llvm::StringRef name);
0740 
0741   /// Create a nullptr value object with the specified type (must be a
0742   /// nullptr type).
0743   static lldb::ValueObjectSP CreateValueObjectFromNullptr(lldb::TargetSP target,
0744                                                           CompilerType type,
0745                                                           llvm::StringRef name);
0746 
0747   lldb::ValueObjectSP Persist();
0748 
0749   /// Returns true if this is a char* or a char[] if it is a char* and
0750   /// check_pointer is true, it also checks that the pointer is valid.
0751   bool IsCStringContainer(bool check_pointer = false);
0752 
0753   std::pair<size_t, bool>
0754   ReadPointedString(lldb::WritableDataBufferSP &buffer_sp, Status &error,
0755                     bool honor_array);
0756 
0757   virtual size_t GetPointeeData(DataExtractor &data, uint32_t item_idx = 0,
0758                                 uint32_t item_count = 1);
0759 
0760   virtual uint64_t GetData(DataExtractor &data, Status &error);
0761 
0762   virtual bool SetData(DataExtractor &data, Status &error);
0763 
0764   virtual bool GetIsConstant() const { return m_update_point.IsConstant(); }
0765 
0766   bool NeedsUpdating() {
0767     const bool accept_invalid_exe_ctx =
0768         (CanUpdateWithInvalidExecutionContext() == eLazyBoolYes);
0769     return m_update_point.NeedsUpdating(accept_invalid_exe_ctx);
0770   }
0771 
0772   void SetIsConstant() { m_update_point.SetIsConstant(); }
0773 
0774   lldb::Format GetFormat() const;
0775 
0776   virtual void SetFormat(lldb::Format format) {
0777     if (format != m_format)
0778       ClearUserVisibleData(eClearUserVisibleDataItemsValue);
0779     m_format = format;
0780   }
0781 
0782   virtual lldb::LanguageType GetPreferredDisplayLanguage();
0783 
0784   void SetPreferredDisplayLanguage(lldb::LanguageType lt) {
0785     m_preferred_display_language = lt;
0786   }
0787 
0788   lldb::TypeSummaryImplSP GetSummaryFormat() {
0789     UpdateFormatsIfNeeded();
0790     return m_type_summary_sp;
0791   }
0792 
0793   void SetSummaryFormat(lldb::TypeSummaryImplSP format) {
0794     m_type_summary_sp = std::move(format);
0795     ClearUserVisibleData(eClearUserVisibleDataItemsSummary);
0796   }
0797 
0798   void SetDerefValobj(ValueObject *deref) { m_deref_valobj = deref; }
0799 
0800   ValueObject *GetDerefValobj() { return m_deref_valobj; }
0801 
0802   void SetValueFormat(lldb::TypeFormatImplSP format) {
0803     m_type_format_sp = std::move(format);
0804     ClearUserVisibleData(eClearUserVisibleDataItemsValue);
0805   }
0806 
0807   lldb::TypeFormatImplSP GetValueFormat() {
0808     UpdateFormatsIfNeeded();
0809     return m_type_format_sp;
0810   }
0811 
0812   void SetSyntheticChildren(const lldb::SyntheticChildrenSP &synth_sp) {
0813     if (synth_sp.get() == m_synthetic_children_sp.get())
0814       return;
0815     ClearUserVisibleData(eClearUserVisibleDataItemsSyntheticChildren);
0816     m_synthetic_children_sp = synth_sp;
0817   }
0818 
0819   lldb::SyntheticChildrenSP GetSyntheticChildren() {
0820     UpdateFormatsIfNeeded();
0821     return m_synthetic_children_sp;
0822   }
0823 
0824   // Use GetParent for display purposes, but if you want to tell the parent to
0825   // update itself then use m_parent.  The ValueObjectDynamicValue's parent is
0826   // not the correct parent for displaying, they are really siblings, so for
0827   // display it needs to route through to its grandparent.
0828   virtual ValueObject *GetParent() { return m_parent; }
0829 
0830   virtual const ValueObject *GetParent() const { return m_parent; }
0831 
0832   ValueObject *GetNonBaseClassParent();
0833 
0834   void SetAddressTypeOfChildren(AddressType at) {
0835     m_address_type_of_ptr_or_ref_children = at;
0836   }
0837 
0838   AddressType GetAddressTypeOfChildren();
0839 
0840   void SetHasCompleteType() {
0841     m_flags.m_did_calculate_complete_objc_class_type = true;
0842   }
0843 
0844   /// Find out if a ValueObject might have children.
0845   ///
0846   /// This call is much more efficient than CalculateNumChildren() as
0847   /// it doesn't need to complete the underlying type. This is designed
0848   /// to be used in a UI environment in order to detect if the
0849   /// disclosure triangle should be displayed or not.
0850   ///
0851   /// This function returns true for class, union, structure,
0852   /// pointers, references, arrays and more. Again, it does so without
0853   /// doing any expensive type completion.
0854   ///
0855   /// \return
0856   ///     Returns \b true if the ValueObject might have children, or \b
0857   ///     false otherwise.
0858   virtual bool MightHaveChildren();
0859 
0860   virtual lldb::VariableSP GetVariable() { return nullptr; }
0861 
0862   virtual bool IsRuntimeSupportValue();
0863 
0864   virtual uint64_t GetLanguageFlags() { return m_language_flags; }
0865 
0866   virtual void SetLanguageFlags(uint64_t flags) { m_language_flags = flags; }
0867 
0868 protected:
0869   typedef ClusterManager<ValueObject> ValueObjectManager;
0870 
0871   class ChildrenManager {
0872   public:
0873     ChildrenManager() = default;
0874 
0875     bool HasChildAtIndex(size_t idx) {
0876       std::lock_guard<std::recursive_mutex> guard(m_mutex);
0877       return (m_children.find(idx) != m_children.end());
0878     }
0879 
0880     ValueObject *GetChildAtIndex(uint32_t idx) {
0881       std::lock_guard<std::recursive_mutex> guard(m_mutex);
0882       const auto iter = m_children.find(idx);
0883       return ((iter == m_children.end()) ? nullptr : iter->second);
0884     }
0885 
0886     void SetChildAtIndex(size_t idx, ValueObject *valobj) {
0887       // we do not need to be mutex-protected to make a pair
0888       ChildrenPair pair(idx, valobj);
0889       std::lock_guard<std::recursive_mutex> guard(m_mutex);
0890       m_children.insert(pair);
0891     }
0892 
0893     void SetChildrenCount(size_t count) { Clear(count); }
0894 
0895     size_t GetChildrenCount() { return m_children_count; }
0896 
0897     void Clear(size_t new_count = 0) {
0898       std::lock_guard<std::recursive_mutex> guard(m_mutex);
0899       m_children_count = new_count;
0900       m_children.clear();
0901     }
0902 
0903   private:
0904     typedef std::map<size_t, ValueObject *> ChildrenMap;
0905     typedef ChildrenMap::iterator ChildrenIterator;
0906     typedef ChildrenMap::value_type ChildrenPair;
0907     std::recursive_mutex m_mutex;
0908     ChildrenMap m_children;
0909     size_t m_children_count = 0;
0910   };
0911 
0912   // Classes that inherit from ValueObject can see and modify these
0913 
0914   /// The parent value object, or nullptr if this has no parent.
0915   ValueObject *m_parent = nullptr;
0916   /// The root of the hierarchy for this ValueObject (or nullptr if never
0917   /// calculated).
0918   ValueObject *m_root = nullptr;
0919   /// Stores both the stop id and the full context at which this value was last
0920   /// updated.  When we are asked to update the value object, we check whether
0921   /// the context & stop id are the same before updating.
0922   EvaluationPoint m_update_point;
0923   /// The name of this object.
0924   ConstString m_name;
0925   /// A data extractor that can be used to extract the value.
0926   DataExtractor m_data;
0927   Value m_value;
0928   /// An error object that can describe any errors that occur when updating
0929   /// values.
0930   Status m_error;
0931   /// Cached value string that will get cleared if/when the value is updated.
0932   std::string m_value_str;
0933   /// Cached old value string from the last time the value was gotten
0934   std::string m_old_value_str;
0935   /// Cached location string that will get cleared if/when the value is updated.
0936   std::string m_location_str;
0937   /// Cached summary string that will get cleared if/when the value is updated.
0938   std::string m_summary_str;
0939   /// Cached result of the "object printer". This differs from the summary
0940   /// in that the summary is consed up by us, the object_desc_string is builtin.
0941   std::string m_object_desc_str;
0942   /// If the type of the value object should be overridden, the type to impose.
0943   CompilerType m_override_type;
0944 
0945   /// This object is managed by the root object (any ValueObject that gets
0946   /// created without a parent.) The manager gets passed through all the
0947   /// generations of dependent objects, and will keep the whole cluster of
0948   /// objects alive as long as a shared pointer to any of them has been handed
0949   /// out. Shared pointers to value objects must always be made with the GetSP
0950   /// method.
0951   ValueObjectManager *m_manager = nullptr;
0952 
0953   ChildrenManager m_children;
0954   std::map<ConstString, ValueObject *> m_synthetic_children;
0955 
0956   ValueObject *m_dynamic_value = nullptr;
0957   ValueObject *m_synthetic_value = nullptr;
0958   ValueObject *m_deref_valobj = nullptr;
0959 
0960   /// We have to hold onto a shared  pointer to this one because it is created
0961   /// as an independent ValueObjectConstResult, which isn't managed by us.
0962   lldb::ValueObjectSP m_addr_of_valobj_sp;
0963 
0964   lldb::Format m_format = lldb::eFormatDefault;
0965   lldb::Format m_last_format = lldb::eFormatDefault;
0966   uint32_t m_last_format_mgr_revision = 0;
0967   lldb::TypeSummaryImplSP m_type_summary_sp;
0968   lldb::TypeFormatImplSP m_type_format_sp;
0969   lldb::SyntheticChildrenSP m_synthetic_children_sp;
0970   ProcessModID m_user_id_of_forced_summary;
0971   AddressType m_address_type_of_ptr_or_ref_children = eAddressTypeInvalid;
0972 
0973   llvm::SmallVector<uint8_t, 16> m_value_checksum;
0974 
0975   lldb::LanguageType m_preferred_display_language = lldb::eLanguageTypeUnknown;
0976 
0977   uint64_t m_language_flags = 0;
0978 
0979   /// Unique identifier for every value object.
0980   UserID m_id;
0981 
0982   // Utility class for initializing all bitfields in ValueObject's constructors.
0983   // FIXME: This could be done via default initializers once we have C++20.
0984   struct Bitflags {
0985     bool m_value_is_valid : 1, m_value_did_change : 1,
0986         m_children_count_valid : 1, m_old_value_valid : 1,
0987         m_is_deref_of_parent : 1, m_is_array_item_for_pointer : 1,
0988         m_is_bitfield_for_scalar : 1, m_is_child_at_offset : 1,
0989         m_is_getting_summary : 1, m_did_calculate_complete_objc_class_type : 1,
0990         m_is_synthetic_children_generated : 1;
0991     Bitflags() {
0992       m_value_is_valid = false;
0993       m_value_did_change = false;
0994       m_children_count_valid = false;
0995       m_old_value_valid = false;
0996       m_is_deref_of_parent = false;
0997       m_is_array_item_for_pointer = false;
0998       m_is_bitfield_for_scalar = false;
0999       m_is_child_at_offset = false;
1000       m_is_getting_summary = false;
1001       m_did_calculate_complete_objc_class_type = false;
1002       m_is_synthetic_children_generated = false;
1003     }
1004   } m_flags;
1005 
1006   friend class ValueObjectChild;
1007   friend class ExpressionVariable; // For SetName
1008   friend class Target;             // For SetName
1009   friend class ValueObjectConstResultImpl;
1010   friend class ValueObjectSynthetic; // For ClearUserVisibleData
1011 
1012   /// Use this constructor to create a "root variable object".  The ValueObject
1013   /// will be locked to this context through-out its lifespan.
1014   ValueObject(ExecutionContextScope *exe_scope, ValueObjectManager &manager,
1015               AddressType child_ptr_or_ref_addr_type = eAddressTypeLoad);
1016 
1017   /// Use this constructor to create a ValueObject owned by another ValueObject.
1018   /// It will inherit the ExecutionContext of its parent.
1019   ValueObject(ValueObject &parent);
1020 
1021   ValueObjectManager *GetManager() { return m_manager; }
1022 
1023   virtual bool UpdateValue() = 0;
1024 
1025   virtual LazyBool CanUpdateWithInvalidExecutionContext() {
1026     return eLazyBoolCalculate;
1027   }
1028 
1029   virtual void CalculateDynamicValue(lldb::DynamicValueType use_dynamic);
1030 
1031   virtual lldb::DynamicValueType GetDynamicValueTypeImpl() {
1032     return lldb::eNoDynamicValues;
1033   }
1034 
1035   virtual bool HasDynamicValueTypeInfo() { return false; }
1036 
1037   virtual void CalculateSyntheticValue();
1038 
1039   /// Should only be called by ValueObject::GetChildAtIndex().
1040   ///
1041   /// \return A ValueObject managed by this ValueObject's manager.
1042   virtual ValueObject *CreateChildAtIndex(size_t idx);
1043 
1044   /// Should only be called by ValueObject::GetSyntheticArrayMember().
1045   ///
1046   /// \return A ValueObject managed by this ValueObject's manager.
1047   virtual ValueObject *CreateSyntheticArrayMember(size_t idx);
1048 
1049   /// Should only be called by ValueObject::GetNumChildren().
1050   virtual llvm::Expected<uint32_t>
1051   CalculateNumChildren(uint32_t max = UINT32_MAX) = 0;
1052 
1053   void SetNumChildren(uint32_t num_children);
1054 
1055   void SetValueDidChange(bool value_changed) {
1056     m_flags.m_value_did_change = value_changed;
1057   }
1058 
1059   void SetValueIsValid(bool valid) { m_flags.m_value_is_valid = valid; }
1060 
1061   void ClearUserVisibleData(
1062       uint32_t items = ValueObject::eClearUserVisibleDataItemsAllStrings);
1063 
1064   void AddSyntheticChild(ConstString key, ValueObject *valobj);
1065 
1066   DataExtractor &GetDataExtractor();
1067 
1068   void ClearDynamicTypeInformation();
1069 
1070   // Subclasses must implement the functions below.
1071 
1072   virtual CompilerType GetCompilerTypeImpl() = 0;
1073 
1074   const char *GetLocationAsCStringImpl(const Value &value,
1075                                        const DataExtractor &data);
1076 
1077   bool IsChecksumEmpty() { return m_value_checksum.empty(); }
1078 
1079   void SetPreferredDisplayLanguageIfNeeded(lldb::LanguageType);
1080 
1081 protected:
1082   virtual void DoUpdateChildrenAddressType(ValueObject &valobj) {};
1083 
1084 private:
1085   virtual CompilerType MaybeCalculateCompleteType();
1086   void UpdateChildrenAddressType() {
1087     GetRoot()->DoUpdateChildrenAddressType(*this);
1088   }
1089 
1090   lldb::ValueObjectSP GetValueForExpressionPath_Impl(
1091       llvm::StringRef expression_cstr,
1092       ExpressionPathScanEndReason *reason_to_stop,
1093       ExpressionPathEndResultType *final_value_type,
1094       const GetValueForExpressionPathOptions &options,
1095       ExpressionPathAftermath *final_task_on_target);
1096 
1097   ValueObject(const ValueObject &) = delete;
1098   const ValueObject &operator=(const ValueObject &) = delete;
1099 };
1100 
1101 } // namespace lldb_private
1102 
1103 #endif // LLDB_VALUEOBJECT_VALUEOBJECT_H