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 CreateValueObjectFromExpression(llvm::StringRef name,
92 llvm::StringRef expression,
93 const ExecutionContext &exe_ctx);
94
96 CreateValueObjectFromAddress(llvm::StringRef name, uint64_t address,
97 const ExecutionContext &exe_ctx,
98 CompilerType type, bool do_deref = true);
99
101 const DataExtractor &data,
102 const ExecutionContext &exe_ctx,
103 CompilerType type);
104
105private:
109};
110
112public:
115
117
118 llvm::Expected<uint32_t> CalculateNumChildren() override { return 0; }
119
120 lldb::ValueObjectSP GetChildAtIndex(uint32_t idx) override { return nullptr; }
121
122 llvm::Expected<size_t> GetIndexOfChildWithName(ConstString name) override {
123 return llvm::createStringErrorV("Type has no child named '{0}'", name);
124 }
125
129
130 bool MightHaveChildren() override { return false; }
131
133
134private:
136 delete;
139};
140
142public:
143 class Flags {
144 public:
145 Flags() = default;
146
147 Flags(const Flags &other) : m_flags(other.m_flags) {}
148
149 Flags(uint32_t value) : m_flags(value) {}
150
151 Flags &operator=(const Flags &rhs) {
152 if (&rhs != this)
153 m_flags = rhs.m_flags;
154
155 return *this;
156 }
157
158 Flags &operator=(const uint32_t &rhs) {
159 m_flags = rhs;
160 return *this;
161 }
162
164 m_flags = 0;
165 return *this;
166 }
167
168 bool GetCascades() const {
169 return (m_flags & lldb::eTypeOptionCascade) == lldb::eTypeOptionCascade;
170 }
171
172 Flags &SetCascades(bool value = true) {
173 if (value)
174 m_flags |= lldb::eTypeOptionCascade;
175 else
176 m_flags &= ~lldb::eTypeOptionCascade;
177 return *this;
178 }
179
180 bool GetSkipPointers() const {
181 return (m_flags & lldb::eTypeOptionSkipPointers) ==
182 lldb::eTypeOptionSkipPointers;
183 }
184
185 Flags &SetSkipPointers(bool value = true) {
186 if (value)
187 m_flags |= lldb::eTypeOptionSkipPointers;
188 else
189 m_flags &= ~lldb::eTypeOptionSkipPointers;
190 return *this;
191 }
192
193 bool GetSkipReferences() const {
194 return (m_flags & lldb::eTypeOptionSkipReferences) ==
195 lldb::eTypeOptionSkipReferences;
196 }
197
198 Flags &SetSkipReferences(bool value = true) {
199 if (value)
200 m_flags |= lldb::eTypeOptionSkipReferences;
201 else
202 m_flags &= ~lldb::eTypeOptionSkipReferences;
203 return *this;
204 }
205
206 bool GetNonCacheable() const {
207 return (m_flags & lldb::eTypeOptionNonCacheable) ==
208 lldb::eTypeOptionNonCacheable;
209 }
210
211 Flags &SetNonCacheable(bool value = true) {
212 if (value)
213 m_flags |= lldb::eTypeOptionNonCacheable;
214 else
215 m_flags &= ~lldb::eTypeOptionNonCacheable;
216 return *this;
217 }
218
220 return (m_flags & lldb::eTypeOptionFrontEndWantsDereference) ==
221 lldb::eTypeOptionFrontEndWantsDereference;
222 }
223
224 Flags &SetFrontEndWantsDereference(bool value = true) {
225 if (value)
226 m_flags |= lldb::eTypeOptionFrontEndWantsDereference;
227 else
228 m_flags &= ~lldb::eTypeOptionFrontEndWantsDereference;
229 return *this;
230 }
231
233 return m_flags & lldb::eTypeOptionCustomSubscripting;
234 }
235
236 Flags &SetCustomSubscripting(bool value = true) {
237 if (value)
238 m_flags |= lldb::eTypeOptionCustomSubscripting;
239 else
240 m_flags &= ~lldb::eTypeOptionCustomSubscripting;
241 return *this;
242 }
243
244 uint32_t GetValue() { return m_flags; }
245
246 void SetValue(uint32_t value) { m_flags = value; }
247
248 private:
249 uint32_t m_flags = lldb::eTypeOptionCascade;
250 };
251
252 SyntheticChildren(const Flags &flags);
253
255
256 bool Cascades() const { return m_flags.GetCascades(); }
257
258 bool SkipsPointers() const { return m_flags.GetSkipPointers(); }
259
260 bool SkipsReferences() const { return m_flags.GetSkipReferences(); }
261
262 bool NonCacheable() const { return m_flags.GetNonCacheable(); }
263
264 bool WantsDereference() const { return m_flags.GetFrontEndWantsDereference();}
265
266 bool CustomSubscripting() const { return m_flags.GetCustomSubscripting(); }
267
268 void SetCascades(bool value) { m_flags.SetCascades(value); }
269
270 void SetSkipsPointers(bool value) { m_flags.SetSkipPointers(value); }
271
272 void SetSkipsReferences(bool value) { m_flags.SetSkipReferences(value); }
273
274 void SetNonCacheable(bool value) { m_flags.SetNonCacheable(value); }
275
276 uint32_t GetOptions() { return m_flags.GetValue(); }
277
278 void SetOptions(uint32_t value) { m_flags.SetValue(value); }
279
280 virtual bool IsScripted() = 0;
281
282 virtual std::string GetDescription() = 0;
283
285 GetFrontEnd(ValueObject &backend) = 0;
286
287 typedef std::shared_ptr<SyntheticChildren> SharedPointer;
288
289 uint32_t &GetRevision() { return m_my_revision; }
290
291 uint32_t GetPtrMatchDepth() { return m_ptr_match_depth; }
292
293 void SetPtrMatchDepth(uint32_t value) { m_ptr_match_depth = value; }
294
295protected:
296 uint32_t m_my_revision = 0;
298 uint32_t m_ptr_match_depth = 1;
299
300private:
303};
304
306 std::vector<std::string> m_expression_paths;
307
308public:
311
313 const std::initializer_list<const char *> items)
314 : SyntheticChildren(flags) {
315 for (auto path : items)
316 AddExpressionPath(path);
317 }
318
319 void AddExpressionPath(const char *path) {
320 AddExpressionPath(std::string(path));
321 }
322
323 void Clear() { m_expression_paths.clear(); }
324
325 size_t GetCount() const { return m_expression_paths.size(); }
326
327 const char *GetExpressionPathAtIndex(size_t i) const {
328 return m_expression_paths[i].c_str();
329 }
330
331 bool SetExpressionPathAtIndex(size_t i, const char *path) {
332 return SetExpressionPathAtIndex(i, std::string(path));
333 }
334
335 void AddExpressionPath(const std::string &path);
336
337 bool SetExpressionPathAtIndex(size_t i, const std::string &path);
338
339 bool IsScripted() override { return false; }
340
341 std::string GetDescription() override;
342
344 public:
346 : SyntheticChildrenFrontEnd(backend), filter(flt) {}
347
348 ~FrontEnd() override = default;
349
350 llvm::Expected<uint32_t> CalculateNumChildren() override {
351 return filter->GetCount();
352 }
353
354 lldb::ValueObjectSP GetChildAtIndex(uint32_t idx) override {
355 if (idx >= filter->GetCount())
356 return lldb::ValueObjectSP();
357 return m_backend.GetSyntheticExpressionPathChild(
358 filter->GetExpressionPathAtIndex(idx), true);
359 }
360
364
365 bool MightHaveChildren() override { return filter->GetCount() > 0; }
366
367 llvm::Expected<size_t> GetIndexOfChildWithName(ConstString name) override;
368
369 typedef std::shared_ptr<SyntheticChildrenFrontEnd> SharedPointer;
370
371 private:
373
374 FrontEnd(const FrontEnd &) = delete;
375 const FrontEnd &operator=(const FrontEnd &) = delete;
376 };
377
379 GetFrontEnd(ValueObject &backend) override {
381 new FrontEnd(this, backend));
382 }
383
384 typedef std::shared_ptr<TypeFilterImpl> SharedPointer;
385
386private:
388 const TypeFilterImpl &operator=(const TypeFilterImpl &) = delete;
389};
390
392public:
393 typedef std::function<SyntheticChildrenFrontEnd *(CXXSyntheticChildren *,
397 const char *description, CreateFrontEndCallback callback);
398
400
401 bool IsScripted() override { return false; }
402
403 std::string GetDescription() override;
404
406 GetFrontEnd(ValueObject &backend) override {
408 m_create_callback(this, backend.GetSP()));
409 }
410
411protected:
413 std::string m_description;
414
415private:
418};
419
421 std::string m_python_class;
422 std::string m_python_code;
423
424public:
426 const char *pclass, const char *pcode = nullptr)
427 : SyntheticChildren(flags) {
428 if (pclass)
429 m_python_class = pclass;
430 if (pcode)
431 m_python_code = pcode;
432 }
433
434 const char *GetPythonClassName() { return m_python_class.c_str(); }
435
436 const char *GetPythonCode() { return m_python_code.c_str(); }
437
438 void SetPythonClassName(const char *fname) {
439 m_python_class.assign(fname);
440 m_python_code.clear();
441 }
442
443 void SetPythonCode(const char *script) { m_python_code.assign(script); }
444
445 std::string GetDescription() override;
446
447 bool IsScripted() override { return true; }
448
450 public:
451 FrontEnd(std::string pclass, ValueObject &backend);
452
453 ~FrontEnd() override;
454
455 bool IsValid();
456
457 llvm::Expected<uint32_t> CalculateNumChildren() override;
458
459 llvm::Expected<uint32_t> CalculateNumChildren(uint32_t max) override;
460
461 lldb::ValueObjectSP GetChildAtIndex(uint32_t idx) override;
462
463 lldb::ChildCacheState Update() override;
464
465 bool MightHaveChildren() override;
466
467 llvm::Expected<size_t> GetIndexOfChildWithName(ConstString name) override;
468
470
472
473 typedef std::shared_ptr<SyntheticChildrenFrontEnd> SharedPointer;
474
475 private:
476 std::string m_python_class;
479
480 FrontEnd(const FrontEnd &) = delete;
481 const FrontEnd &operator=(const FrontEnd &) = delete;
482 };
483
485 GetFrontEnd(ValueObject &backend) override {
487 new FrontEnd(m_python_class, backend));
488 if (synth_ptr && ((FrontEnd *)synth_ptr.get())->IsValid())
489 return synth_ptr;
490 return nullptr;
491 }
492
493private:
497};
498
499/// A synthetic formatter that is defined in LLDB formmater bytecode.
500///
501/// See `BytecodeSummaryFormat` for the corresponding summary formatter.
502///
503/// Formatter bytecode documentation can be found in
504/// lldb/docs/resources/formatterbytecode.rst
506public:
508 std::unique_ptr<llvm::MemoryBuffer> init;
509 std::unique_ptr<llvm::MemoryBuffer> update;
510 std::unique_ptr<llvm::MemoryBuffer> num_children;
511 std::unique_ptr<llvm::MemoryBuffer> get_child_at_index;
512 std::unique_ptr<llvm::MemoryBuffer> get_child_index;
513 };
514
515private:
517 public:
519
520 lldb::ChildCacheState Update() override;
521 llvm::Expected<uint32_t> CalculateNumChildren() override;
522 lldb::ValueObjectSP GetChildAtIndex(uint32_t idx) override;
523 llvm::Expected<size_t> GetIndexOfChildWithName(ConstString name) override;
524
525 private:
529 };
530
531public:
534
535 bool IsScripted() override { return false; }
536
537 std::string GetDescription() override;
538
540 GetFrontEnd(ValueObject &backend) override {
542 new FrontEnd(backend, m_impl));
543 }
544
545private:
547};
548
549} // namespace lldb_private
550
551#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 CreateValueObjectFromExpression(llvm::StringRef name, llvm::StringRef expression, const ExecutionContext &exe_ctx)
std::unique_ptr< SyntheticChildrenFrontEnd > UniquePointer
virtual lldb::ValueObjectSP GetSyntheticValue()
virtual llvm::Expected< uint32_t > CalculateNumChildren(uint32_t max)
lldb::ValueObjectSP CreateValueObjectFromData(llvm::StringRef name, const DataExtractor &data, const ExecutionContext &exe_ctx, CompilerType type)
std::shared_ptr< SyntheticChildrenFrontEnd > SharedPointer
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 CreateValueObjectFromAddress(llvm::StringRef name, uint64_t address, const ExecutionContext &exe_ctx, CompilerType type, bool do_deref=true)
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