LLDB mainline
TypeSynthetic.h
Go to the documentation of this file.
1//===-- TypeSynthetic.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_DATAFORMATTERS_TYPESYNTHETIC_H
10#define LLDB_DATAFORMATTERS_TYPESYNTHETIC_H
11
12#include <cstdint>
13
14#include <functional>
15#include <initializer_list>
16#include <memory>
17#include <string>
18#include <vector>
19
21#include "lldb/lldb-public.h"
22
26#include "llvm/Support/ErrorExtras.h"
27
28namespace lldb_private {
30protected:
32
33public:
35
36 virtual ~SyntheticChildrenFrontEnd() = default;
37
38 virtual llvm::Expected<uint32_t> CalculateNumChildren() = 0;
39
40 virtual llvm::Expected<uint32_t> CalculateNumChildren(uint32_t max) {
41 auto count = CalculateNumChildren();
42 if (!count)
43 return count;
44 return *count <= max ? *count : max;
45 }
46
47 uint32_t CalculateNumChildrenIgnoringErrors(uint32_t max = UINT32_MAX);
48
49 virtual lldb::ValueObjectSP GetChildAtIndex(uint32_t idx) = 0;
50
51 /// Determine the index of a named child. Subscript names ("[N]") are, by
52 /// default, handled automatically. For data types which need custom
53 /// subscripting behavior - for example a sparse array, disable automatic
54 /// subscripting with TypeOptions::eTypeOptionCustomSubscripting.
55 virtual llvm::Expected<size_t> GetIndexOfChildWithName(ConstString name) {
56 return llvm::createStringErrorV("Type has no child named '{0}'", name);
57 }
58
59 /// This function is assumed to always succeed and if it fails, the front-end
60 /// should know to deal with it in the correct way (most probably, by refusing
61 /// to return any children). The return value of \ref Update should actually
62 /// be interpreted as "ValueObjectSynthetic cache is good/bad". If this
63 /// function returns \ref lldb::ChildCacheState::eReuse, \ref
64 /// ValueObjectSynthetic is allowed to use the children it fetched
65 /// previously and cached. Otherwise, \ref ValueObjectSynthetic must
66 /// throw away its cache, and query again for children.
68
69 // if this function returns false, then CalculateNumChildren() MUST return 0
70 // since UI frontends might validly decide not to inquire for children given
71 // a false return value from this call if it returns true, then
72 // CalculateNumChildren() can return any number >= 0 (0 being valid) it
73 // should if at all possible be more efficient than CalculateNumChildren()
74 virtual bool MightHaveChildren() { return true; }
75
76 // if this function returns a non-null ValueObject, then the returned
77 // ValueObject will stand for this ValueObject whenever a "value" request is
78 // made to this ValueObject
79 virtual lldb::ValueObjectSP GetSyntheticValue() { return nullptr; }
80
81 // if this function returns a non-empty ConstString, then clients are
82 // expected to use the return as the name of the type of this ValueObject for
83 // display purposes
85
86 typedef std::shared_ptr<SyntheticChildrenFrontEnd> SharedPointer;
87 typedef std::unique_ptr<SyntheticChildrenFrontEnd> UniquePointer;
88
89protected:
91 CreateChildValueObjectFromExpression(llvm::StringRef name,
92 llvm::StringRef expression,
93 const ExecutionContext &exe_ctx);
94
96 CreateChildValueObjectFromAddress(llvm::StringRef name, uint64_t address,
97 const ExecutionContext &exe_ctx,
98 CompilerType type, bool do_deref = true);
99
101 llvm::StringRef name, const DataExtractor &data,
102 const ExecutionContext &exe_ctx, CompilerType type);
103
104private:
108};
109
111public:
114
116
117 llvm::Expected<uint32_t> CalculateNumChildren() override { return 0; }
118
119 lldb::ValueObjectSP GetChildAtIndex(uint32_t idx) override { return nullptr; }
120
121 llvm::Expected<size_t> GetIndexOfChildWithName(ConstString name) override {
122 return llvm::createStringErrorV("Type has no child named '{0}'", name);
123 }
124
128
129 bool MightHaveChildren() override { return false; }
130
132
133private:
135 delete;
138};
139
141public:
142 class Flags {
143 public:
144 Flags() = default;
145
146 Flags(const Flags &other) : m_flags(other.m_flags) {}
147
148 Flags(uint32_t value) : m_flags(value) {}
149
150 Flags &operator=(const Flags &rhs) {
151 if (&rhs != this)
152 m_flags = rhs.m_flags;
153
154 return *this;
155 }
156
157 Flags &operator=(const uint32_t &rhs) {
158 m_flags = rhs;
159 return *this;
160 }
161
163 m_flags = 0;
164 return *this;
165 }
166
167 bool GetCascades() const {
168 return (m_flags & lldb::eTypeOptionCascade) == lldb::eTypeOptionCascade;
169 }
170
171 Flags &SetCascades(bool value = true) {
172 if (value)
173 m_flags |= lldb::eTypeOptionCascade;
174 else
175 m_flags &= ~lldb::eTypeOptionCascade;
176 return *this;
177 }
178
179 bool GetSkipPointers() const {
180 return (m_flags & lldb::eTypeOptionSkipPointers) ==
181 lldb::eTypeOptionSkipPointers;
182 }
183
184 Flags &SetSkipPointers(bool value = true) {
185 if (value)
186 m_flags |= lldb::eTypeOptionSkipPointers;
187 else
188 m_flags &= ~lldb::eTypeOptionSkipPointers;
189 return *this;
190 }
191
192 bool GetSkipReferences() const {
193 return (m_flags & lldb::eTypeOptionSkipReferences) ==
194 lldb::eTypeOptionSkipReferences;
195 }
196
197 Flags &SetSkipReferences(bool value = true) {
198 if (value)
199 m_flags |= lldb::eTypeOptionSkipReferences;
200 else
201 m_flags &= ~lldb::eTypeOptionSkipReferences;
202 return *this;
203 }
204
205 bool GetNonCacheable() const {
206 return (m_flags & lldb::eTypeOptionNonCacheable) ==
207 lldb::eTypeOptionNonCacheable;
208 }
209
210 Flags &SetNonCacheable(bool value = true) {
211 if (value)
212 m_flags |= lldb::eTypeOptionNonCacheable;
213 else
214 m_flags &= ~lldb::eTypeOptionNonCacheable;
215 return *this;
216 }
217
219 return (m_flags & lldb::eTypeOptionFrontEndWantsDereference) ==
220 lldb::eTypeOptionFrontEndWantsDereference;
221 }
222
223 Flags &SetFrontEndWantsDereference(bool value = true) {
224 if (value)
225 m_flags |= lldb::eTypeOptionFrontEndWantsDereference;
226 else
227 m_flags &= ~lldb::eTypeOptionFrontEndWantsDereference;
228 return *this;
229 }
230
232 return m_flags & lldb::eTypeOptionCustomSubscripting;
233 }
234
235 Flags &SetCustomSubscripting(bool value = true) {
236 if (value)
237 m_flags |= lldb::eTypeOptionCustomSubscripting;
238 else
239 m_flags &= ~lldb::eTypeOptionCustomSubscripting;
240 return *this;
241 }
242
243 uint32_t GetValue() { return m_flags; }
244
245 void SetValue(uint32_t value) { m_flags = value; }
246
247 private:
248 uint32_t m_flags = lldb::eTypeOptionCascade;
249 };
250
251 SyntheticChildren(const Flags &flags);
252
254
255 bool Cascades() const { return m_flags.GetCascades(); }
256
257 bool SkipsPointers() const { return m_flags.GetSkipPointers(); }
258
259 bool SkipsReferences() const { return m_flags.GetSkipReferences(); }
260
261 bool NonCacheable() const { return m_flags.GetNonCacheable(); }
262
263 bool WantsDereference() const { return m_flags.GetFrontEndWantsDereference();}
264
265 bool CustomSubscripting() const { return m_flags.GetCustomSubscripting(); }
266
267 void SetCascades(bool value) { m_flags.SetCascades(value); }
268
269 void SetSkipsPointers(bool value) { m_flags.SetSkipPointers(value); }
270
271 void SetSkipsReferences(bool value) { m_flags.SetSkipReferences(value); }
272
273 void SetNonCacheable(bool value) { m_flags.SetNonCacheable(value); }
274
275 uint32_t GetOptions() { return m_flags.GetValue(); }
276
277 void SetOptions(uint32_t value) { m_flags.SetValue(value); }
278
279 virtual bool IsScripted() = 0;
280
281 virtual std::string GetDescription() = 0;
282
284 GetFrontEnd(ValueObject &backend) = 0;
285
286 typedef std::shared_ptr<SyntheticChildren> SharedPointer;
287
288 uint32_t &GetRevision() { return m_my_revision; }
289
290 uint32_t GetPtrMatchDepth() { return m_ptr_match_depth; }
291
292 void SetPtrMatchDepth(uint32_t value) { m_ptr_match_depth = value; }
293
294protected:
295 uint32_t m_my_revision = 0;
297 uint32_t m_ptr_match_depth = 1;
298
299private:
302};
303
305 std::vector<std::string> m_expression_paths;
306
307public:
310
312 const std::initializer_list<const char *> items)
313 : SyntheticChildren(flags) {
314 for (auto path : items)
315 AddExpressionPath(path);
316 }
317
318 void AddExpressionPath(const char *path) {
319 AddExpressionPath(std::string(path));
320 }
321
322 void Clear() { m_expression_paths.clear(); }
323
324 size_t GetCount() const { return m_expression_paths.size(); }
325
326 const char *GetExpressionPathAtIndex(size_t i) const {
327 return m_expression_paths[i].c_str();
328 }
329
330 bool SetExpressionPathAtIndex(size_t i, const char *path) {
331 return SetExpressionPathAtIndex(i, std::string(path));
332 }
333
334 void AddExpressionPath(const std::string &path);
335
336 bool SetExpressionPathAtIndex(size_t i, const std::string &path);
337
338 bool IsScripted() override { return false; }
339
340 std::string GetDescription() override;
341
343 public:
345 : SyntheticChildrenFrontEnd(backend), filter(flt) {}
346
347 ~FrontEnd() override = default;
348
349 llvm::Expected<uint32_t> CalculateNumChildren() override {
350 return filter->GetCount();
351 }
352
353 lldb::ValueObjectSP GetChildAtIndex(uint32_t idx) override {
354 if (idx >= filter->GetCount())
355 return lldb::ValueObjectSP();
356 return m_backend.GetSyntheticExpressionPathChild(
357 filter->GetExpressionPathAtIndex(idx), true);
358 }
359
363
364 bool MightHaveChildren() override { return filter->GetCount() > 0; }
365
366 llvm::Expected<size_t> GetIndexOfChildWithName(ConstString name) override;
367
368 typedef std::shared_ptr<SyntheticChildrenFrontEnd> SharedPointer;
369
370 private:
372
373 FrontEnd(const FrontEnd &) = delete;
374 const FrontEnd &operator=(const FrontEnd &) = delete;
375 };
376
378 GetFrontEnd(ValueObject &backend) override {
380 new FrontEnd(this, backend));
381 }
382
383 typedef std::shared_ptr<TypeFilterImpl> SharedPointer;
384
385private:
387 const TypeFilterImpl &operator=(const TypeFilterImpl &) = delete;
388};
389
391public:
392 typedef std::function<SyntheticChildrenFrontEnd *(CXXSyntheticChildren *,
396 const char *description, CreateFrontEndCallback callback);
397
399
400 bool IsScripted() override { return false; }
401
402 std::string GetDescription() override;
403
405 GetFrontEnd(ValueObject &backend) override {
407 m_create_callback(this, backend.GetSP()));
408 }
409
410protected:
412 std::string m_description;
413
414private:
417};
418
420 std::string m_python_class;
421 std::string m_python_code;
422
423public:
425 const char *pclass, const char *pcode = nullptr)
426 : SyntheticChildren(flags) {
427 if (pclass)
428 m_python_class = pclass;
429 if (pcode)
430 m_python_code = pcode;
431 }
432
433 const char *GetPythonClassName() { return m_python_class.c_str(); }
434
435 const char *GetPythonCode() { return m_python_code.c_str(); }
436
437 void SetPythonClassName(const char *fname) {
438 m_python_class.assign(fname);
439 m_python_code.clear();
440 }
441
442 void SetPythonCode(const char *script) { m_python_code.assign(script); }
443
444 std::string GetDescription() override;
445
446 bool IsScripted() override { return true; }
447
449 public:
450 FrontEnd(std::string pclass, ValueObject &backend);
451
452 ~FrontEnd() override;
453
454 bool IsValid();
455
456 llvm::Expected<uint32_t> CalculateNumChildren() override;
457
458 llvm::Expected<uint32_t> CalculateNumChildren(uint32_t max) override;
459
460 lldb::ValueObjectSP GetChildAtIndex(uint32_t idx) override;
461
462 lldb::ChildCacheState Update() override;
463
464 bool MightHaveChildren() override;
465
466 llvm::Expected<size_t> GetIndexOfChildWithName(ConstString name) override;
467
469
471
472 typedef std::shared_ptr<SyntheticChildrenFrontEnd> SharedPointer;
473
474 private:
475 std::string m_python_class;
478
479 FrontEnd(const FrontEnd &) = delete;
480 const FrontEnd &operator=(const FrontEnd &) = delete;
481 };
482
484 GetFrontEnd(ValueObject &backend) override {
486 new FrontEnd(m_python_class, backend));
487 if (synth_ptr && ((FrontEnd *)synth_ptr.get())->IsValid())
488 return synth_ptr;
489 return nullptr;
490 }
491
492private:
496};
497
498/// A synthetic formatter that is defined in LLDB formmater bytecode.
499///
500/// See `BytecodeSummaryFormat` for the corresponding summary formatter.
501///
502/// Formatter bytecode documentation can be found in
503/// lldb/docs/resources/formatterbytecode.rst
505public:
507 std::unique_ptr<llvm::MemoryBuffer> init;
508 std::unique_ptr<llvm::MemoryBuffer> update;
509 std::unique_ptr<llvm::MemoryBuffer> num_children;
510 std::unique_ptr<llvm::MemoryBuffer> get_child_at_index;
511 std::unique_ptr<llvm::MemoryBuffer> get_child_index;
512 };
513
514private:
516 public:
518
519 lldb::ChildCacheState Update() override;
520 llvm::Expected<uint32_t> CalculateNumChildren() override;
521 lldb::ValueObjectSP GetChildAtIndex(uint32_t idx) override;
522 llvm::Expected<size_t> GetIndexOfChildWithName(ConstString name) override;
523
524 private:
528 };
529
530public:
533
534 bool IsScripted() override { return false; }
535
536 std::string GetDescription() override;
537
539 GetFrontEnd(ValueObject &backend) override {
541 new FrontEnd(backend, m_impl));
542 }
543
544private:
546};
547
548} // namespace lldb_private
549
550#endif // LLDB_DATAFORMATTERS_TYPESYNTHETIC_H
FrontEnd(ValueObject &backend, SyntheticBytecodeImplementation &impl)
lldb::ValueObjectSP GetChildAtIndex(uint32_t idx) override
llvm::Expected< size_t > GetIndexOfChildWithName(ConstString name) override
Determine the index of a named child.
llvm::Expected< uint32_t > CalculateNumChildren() override
const SyntheticBytecodeImplementation & m_impl
lldb::ChildCacheState Update() override
This function is assumed to always succeed and if it fails, the front-end should know to deal with it...
SyntheticChildrenFrontEnd::UniquePointer GetFrontEnd(ValueObject &backend) override
BytecodeSyntheticChildren(SyntheticBytecodeImplementation &&impl)
SyntheticBytecodeImplementation m_impl
CXXSyntheticChildren(const CXXSyntheticChildren &)=delete
const CXXSyntheticChildren & operator=(const CXXSyntheticChildren &)=delete
CreateFrontEndCallback m_create_callback
std::string GetDescription() override
SyntheticChildrenFrontEnd::UniquePointer GetFrontEnd(ValueObject &backend) override
CXXSyntheticChildren(const SyntheticChildren::Flags &flags, const char *description, CreateFrontEndCallback callback)
std::function< SyntheticChildrenFrontEnd *(CXXSyntheticChildren *, lldb::ValueObjectSP)> CreateFrontEndCallback
Generic representation of a type in a programming language.
A uniqued constant string class.
Definition ConstString.h:40
An data extractor class.
"lldb/Target/ExecutionContext.h" A class that contains an execution context.
lldb::ChildCacheState Update() override
This function is assumed to always succeed and if it fails, the front-end should know to deal with it...
llvm::Expected< size_t > GetIndexOfChildWithName(ConstString name) override
Determine the index of a named child.
lldb::ValueObjectSP GetChildAtIndex(uint32_t idx) override
std::shared_ptr< SyntheticChildrenFrontEnd > SharedPointer
FrontEnd(std::string pclass, ValueObject &backend)
llvm::Expected< uint32_t > CalculateNumChildren() override
const FrontEnd & operator=(const FrontEnd &)=delete
SyntheticChildrenFrontEnd::UniquePointer GetFrontEnd(ValueObject &backend) override
void SetPythonClassName(const char *fname)
ScriptedSyntheticChildren(const SyntheticChildren::Flags &flags, const char *pclass, const char *pcode=nullptr)
const ScriptedSyntheticChildren & operator=(const ScriptedSyntheticChildren &)=delete
void SetPythonCode(const char *script)
ScriptedSyntheticChildren(const ScriptedSyntheticChildren &)=delete
std::shared_ptr< Object > ObjectSP
uint32_t CalculateNumChildrenIgnoringErrors(uint32_t max=UINT32_MAX)
virtual lldb::ValueObjectSP GetChildAtIndex(uint32_t idx)=0
virtual lldb::ChildCacheState Update()=0
This function is assumed to always succeed and if it fails, the front-end should know to deal with it...
lldb::ValueObjectSP CreateChildValueObjectFromAddress(llvm::StringRef name, uint64_t address, const ExecutionContext &exe_ctx, CompilerType type, bool do_deref=true)
std::unique_ptr< SyntheticChildrenFrontEnd > UniquePointer
virtual lldb::ValueObjectSP GetSyntheticValue()
virtual llvm::Expected< uint32_t > CalculateNumChildren(uint32_t max)
std::shared_ptr< SyntheticChildrenFrontEnd > SharedPointer
lldb::ValueObjectSP CreateChildValueObjectFromExpression(llvm::StringRef name, llvm::StringRef expression, const ExecutionContext &exe_ctx)
virtual llvm::Expected< uint32_t > CalculateNumChildren()=0
virtual ConstString GetSyntheticTypeName()
SyntheticChildrenFrontEnd(const SyntheticChildrenFrontEnd &)=delete
SyntheticChildrenFrontEnd(ValueObject &backend)
virtual llvm::Expected< size_t > GetIndexOfChildWithName(ConstString name)
Determine the index of a named child.
const SyntheticChildrenFrontEnd & operator=(const SyntheticChildrenFrontEnd &)=delete
lldb::ValueObjectSP CreateChildValueObjectFromData(llvm::StringRef name, const DataExtractor &data, const ExecutionContext &exe_ctx, CompilerType type)
Flags & SetSkipReferences(bool value=true)
Flags & SetFrontEndWantsDereference(bool value=true)
Flags & operator=(const uint32_t &rhs)
Flags & SetSkipPointers(bool value=true)
Flags & SetCascades(bool value=true)
Flags & SetCustomSubscripting(bool value=true)
Flags & SetNonCacheable(bool value=true)
Flags & operator=(const Flags &rhs)
const SyntheticChildren & operator=(const SyntheticChildren &)=delete
virtual std::string GetDescription()=0
virtual SyntheticChildrenFrontEnd::UniquePointer GetFrontEnd(ValueObject &backend)=0
void SetPtrMatchDepth(uint32_t value)
SyntheticChildren(const Flags &flags)
void SetOptions(uint32_t value)
SyntheticChildren(const SyntheticChildren &)=delete
std::shared_ptr< SyntheticChildren > SharedPointer
llvm::Expected< size_t > GetIndexOfChildWithName(ConstString name) override
Determine the index of a named child.
lldb::ValueObjectSP GetChildAtIndex(uint32_t idx) override
lldb::ChildCacheState Update() override
This function is assumed to always succeed and if it fails, the front-end should know to deal with it...
llvm::Expected< uint32_t > CalculateNumChildren() override
SyntheticValueProviderFrontEnd(const SyntheticValueProviderFrontEnd &)=delete
lldb::ValueObjectSP GetSyntheticValue() override=0
const SyntheticValueProviderFrontEnd & operator=(const SyntheticValueProviderFrontEnd &)=delete
lldb::ChildCacheState Update() override
This function is assumed to always succeed and if it fails, the front-end should know to deal with it...
FrontEnd(const FrontEnd &)=delete
llvm::Expected< size_t > GetIndexOfChildWithName(ConstString name) override
Determine the index of a named child.
llvm::Expected< uint32_t > CalculateNumChildren() override
FrontEnd(TypeFilterImpl *flt, ValueObject &backend)
const FrontEnd & operator=(const FrontEnd &)=delete
std::shared_ptr< SyntheticChildrenFrontEnd > SharedPointer
lldb::ValueObjectSP GetChildAtIndex(uint32_t idx) override
std::shared_ptr< TypeFilterImpl > SharedPointer
bool SetExpressionPathAtIndex(size_t i, const char *path)
const char * GetExpressionPathAtIndex(size_t i) const
TypeFilterImpl(const SyntheticChildren::Flags &flags, const std::initializer_list< const char * > items)
void AddExpressionPath(const char *path)
std::string GetDescription() override
TypeFilterImpl(const SyntheticChildren::Flags &flags)
SyntheticChildrenFrontEnd::UniquePointer GetFrontEnd(ValueObject &backend) override
std::vector< std::string > m_expression_paths
const TypeFilterImpl & operator=(const TypeFilterImpl &)=delete
TypeFilterImpl(const TypeFilterImpl &)=delete
lldb::ValueObjectSP GetSP()
#define UINT32_MAX
A class that represents a running process on the host machine.
ChildCacheState
Specifies if children need to be re-computed after a call to SyntheticChildrenFrontEnd::Update.
@ eRefetch
Children need to be recomputed dynamically.
std::shared_ptr< lldb_private::ValueObject > ValueObjectSP