1//===-- ValueObjectSyntheticFilter.h ----------------------------*- C++ -*-===//
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#ifndef LLDB_CORE_VALUEOBJECTSYNTHETICFILTER_H
10#define LLDB_CORE_VALUEOBJECTSYNTHETICFILTER_H
11
12#include "lldb/Core/ValueObject.h"
13#include "lldb/Symbol/CompilerType.h"
14#include "lldb/Utility/ConstString.h"
15#include "lldb/lldb-defines.h"
16#include "lldb/lldb-enumerations.h"
17#include "lldb/lldb-forward.h"
18#include "lldb/lldb-private-enumerations.h"
19
20#include <cstdint>
21#include <memory>
22#include <optional>
23
24#include <cstddef>
25
26namespace lldb_private {
27class Declaration;
28class Status;
29class SyntheticChildrenFrontEnd;
30
31/// A ValueObject that obtains its children from some source other than
32/// real information.
33/// This is currently used to implement Python-based children and filters but
34/// you can bind it to any source of synthetic information and have it behave
35/// accordingly.
36class ValueObjectSynthetic : public ValueObject {
37public:
38  ~ValueObjectSynthetic() override;
39
40  std::optional<uint64_t> GetByteSize() override;
41
42  ConstString GetTypeName() override;
43
44  ConstString GetQualifiedTypeName() override;
45
46  ConstString GetDisplayTypeName() override;
47
48  bool MightHaveChildren() override;
49
50  size_t CalculateNumChildren(uint32_t max) override;
51
52  lldb::ValueType GetValueType() const override;
53
54  lldb::ValueObjectSP GetChildAtIndex(size_t idx, bool can_create) override;
55
56  lldb::ValueObjectSP GetChildMemberWithName(ConstString name,
57                                             bool can_create) override;
58
59  size_t GetIndexOfChildWithName(ConstString name) override;
60
61  lldb::ValueObjectSP
62  GetDynamicValue(lldb::DynamicValueType valueType) override;
63
64  bool IsInScope() override;
65
66  bool HasSyntheticValue() override { return false; }
67
68  bool IsSynthetic() override { return true; }
69
70  void CalculateSyntheticValue() override {}
71
72  bool IsDynamic() override {
73    return ((m_parent != nullptr) ? m_parent->IsDynamic() : false);
74  }
75
76  lldb::ValueObjectSP GetStaticValue() override {
77    return ((m_parent != nullptr) ? m_parent->GetStaticValue() : GetSP());
78  }
79
80  virtual lldb::DynamicValueType GetDynamicValueType() {
81    return ((m_parent != nullptr) ? m_parent->GetDynamicValueType()
82                                  : lldb::eNoDynamicValues);
83  }
84
85  ValueObject *GetParent() override {
86    return ((m_parent != nullptr) ? m_parent->GetParent() : nullptr);
87  }
88
89  const ValueObject *GetParent() const override {
90    return ((m_parent != nullptr) ? m_parent->GetParent() : nullptr);
91  }
92
93  lldb::ValueObjectSP GetNonSyntheticValue() override;
94
95  bool CanProvideValue() override;
96
97  bool DoesProvideSyntheticValue() override {
98    return (UpdateValueIfNeeded(), m_provides_value == eLazyBoolYes);
99  }
100
101  bool GetIsConstant() const override { return false; }
102
103  bool SetValueFromCString(const char *value_str, Status &error) override;
104
105  void SetFormat(lldb::Format format) override;
106
107  lldb::LanguageType GetPreferredDisplayLanguage() override;
108
109  void SetPreferredDisplayLanguage(lldb::LanguageType);
110
111  bool IsSyntheticChildrenGenerated() override;
112
113  void SetSyntheticChildrenGenerated(bool b) override;
114
115  bool GetDeclaration(Declaration &decl) override;
116
117  uint64_t GetLanguageFlags() override;
118
119  void SetLanguageFlags(uint64_t flags) override;
120
121protected:
122  bool UpdateValue() override;
123
124  LazyBool CanUpdateWithInvalidExecutionContext() override {
125    return eLazyBoolYes;
126  }
127
128  CompilerType GetCompilerTypeImpl() override;
129
130  virtual void CreateSynthFilter();
131
132  // we need to hold on to the SyntheticChildren because someone might delete
133  // the type binding while we are alive
134  lldb::SyntheticChildrenSP m_synth_sp;
135  std::unique_ptr<SyntheticChildrenFrontEnd> m_synth_filter_up;
136
137  typedef std::map<uint32_t, ValueObject *> ByIndexMap;
138  typedef std::map<const char *, uint32_t> NameToIndexMap;
139  typedef std::vector<lldb::ValueObjectSP> SyntheticChildrenCache;
140
141  typedef ByIndexMap::iterator ByIndexIterator;
142  typedef NameToIndexMap::iterator NameToIndexIterator;
143
144  std::mutex m_child_mutex;
145  /// Guarded by m_child_mutex;
146  ByIndexMap m_children_byindex;
147  /// Guarded by m_child_mutex;
148  NameToIndexMap m_name_toindex;
149  /// Guarded by m_child_mutex;
150  SyntheticChildrenCache m_synthetic_children_cache;
151
152  // FIXME: use the ValueObject's  ChildrenManager instead of a special purpose
153  // solution.
154  uint32_t m_synthetic_children_count;
155
156  ConstString m_parent_type_name;
157
158  LazyBool m_might_have_children;
159
160  LazyBool m_provides_value;
161
162private:
163  friend class ValueObject;
164  ValueObjectSynthetic(ValueObject &parent, lldb::SyntheticChildrenSP filter);
165
166  void CopyValueData(ValueObject *source);
167
168  ValueObjectSynthetic(const ValueObjectSynthetic &) = delete;
169  const ValueObjectSynthetic &operator=(const ValueObjectSynthetic &) = delete;
170};
171
172} // namespace lldb_private
173
174#endif // LLDB_CORE_VALUEOBJECTSYNTHETICFILTER_H
175