]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/llvm/tools/lldb/include/lldb/DataFormatters/TypeSynthetic.h
Integrate tools/regression/acltools into tests/sys/acl
[FreeBSD/FreeBSD.git] / contrib / llvm / tools / lldb / include / lldb / DataFormatters / TypeSynthetic.h
1 //===-- TypeSynthetic.h -------------------------------------------*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9
10 #ifndef lldb_TypeSynthetic_h_
11 #define lldb_TypeSynthetic_h_
12
13 // C Includes
14 #include <stdint.h>
15
16 // C++ Includes
17 #include <string>
18 #include <vector>
19
20 // Other libraries and framework includes
21
22 // Project includes
23 #include "lldb/lldb-public.h"
24 #include "lldb/lldb-enumerations.h"
25
26 #include "lldb/Core/StructuredData.h"
27 #include "lldb/Core/ValueObject.h"
28
29 namespace lldb_private {
30     class SyntheticChildrenFrontEnd
31     {
32     protected:
33         ValueObject &m_backend;
34         
35         void
36         SetValid (bool valid)
37         {
38             m_valid = valid;
39         }
40         
41         bool
42         IsValid ()
43         {
44             return m_valid;
45         }
46         
47     public:
48         
49         SyntheticChildrenFrontEnd (ValueObject &backend) :
50         m_backend(backend),
51         m_valid(true)
52         {}
53         
54         virtual
55         ~SyntheticChildrenFrontEnd ()
56         {
57         }
58         
59         virtual size_t
60         CalculateNumChildren () = 0;
61         
62         virtual lldb::ValueObjectSP
63         GetChildAtIndex (size_t idx) = 0;
64         
65         virtual size_t
66         GetIndexOfChildWithName (const ConstString &name) = 0;
67         
68         // this function is assumed to always succeed and it if fails, the front-end should know to deal
69         // with it in the correct way (most probably, by refusing to return any children)
70         // the return value of Update() should actually be interpreted as "ValueObjectSyntheticFilter cache is good/bad"
71         // if =true, ValueObjectSyntheticFilter is allowed to use the children it fetched previously and cached
72         // if =false, ValueObjectSyntheticFilter must throw away its cache, and query again for children
73         virtual bool
74         Update () = 0;
75         
76         // if this function returns false, then CalculateNumChildren() MUST return 0 since UI frontends
77         // might validly decide not to inquire for children given a false return value from this call
78         // if it returns true, then CalculateNumChildren() can return any number >= 0 (0 being valid)
79         // it should if at all possible be more efficient than CalculateNumChildren()
80         virtual bool
81         MightHaveChildren () = 0;
82         
83         // if this function returns a non-null ValueObject, then the returned ValueObject will stand
84         // for this ValueObject whenever a "value" request is made to this ValueObject
85         virtual lldb::ValueObjectSP
86         GetSyntheticValue () { return nullptr; }
87         
88         typedef std::shared_ptr<SyntheticChildrenFrontEnd> SharedPointer;
89         typedef std::unique_ptr<SyntheticChildrenFrontEnd> AutoPointer;
90         
91     protected:
92         lldb::ValueObjectSP
93         CreateValueObjectFromExpression (const char* name,
94                                          const char* expression,
95                                          const ExecutionContext& exe_ctx);
96         
97         lldb::ValueObjectSP
98         CreateValueObjectFromAddress (const char* name,
99                                       uint64_t address,
100                                       const ExecutionContext& exe_ctx,
101                                       ClangASTType type);
102         
103         lldb::ValueObjectSP
104         CreateValueObjectFromData (const char* name,
105                                    const DataExtractor& data,
106                                    const ExecutionContext& exe_ctx,
107                                    ClangASTType type);
108         
109     private:
110         bool m_valid;
111         DISALLOW_COPY_AND_ASSIGN(SyntheticChildrenFrontEnd);
112     };
113     
114     class SyntheticValueProviderFrontEnd : public SyntheticChildrenFrontEnd
115     {
116     public:
117         SyntheticValueProviderFrontEnd (ValueObject &backend) :
118         SyntheticChildrenFrontEnd(backend)
119         {}
120         
121         virtual
122         ~SyntheticValueProviderFrontEnd ()
123         {
124         }
125         
126         virtual size_t
127         CalculateNumChildren () { return 0; }
128         
129         virtual lldb::ValueObjectSP
130         GetChildAtIndex (size_t idx) { return nullptr; }
131         
132         virtual size_t
133         GetIndexOfChildWithName (const ConstString &name) { return UINT32_MAX; }
134         
135         virtual bool
136         Update () { return false; }
137         
138         virtual bool
139         MightHaveChildren () { return false; }
140         
141         virtual lldb::ValueObjectSP
142         GetSyntheticValue () = 0;
143         
144     private:
145         DISALLOW_COPY_AND_ASSIGN(SyntheticValueProviderFrontEnd);
146     };
147     
148     class SyntheticChildren
149     {
150     public:
151         
152         class Flags
153         {
154         public:
155             
156             Flags () :
157             m_flags (lldb::eTypeOptionCascade)
158             {}
159             
160             Flags (const Flags& other) :
161             m_flags (other.m_flags)
162             {}
163             
164             Flags (uint32_t value) :
165             m_flags (value)
166             {}
167             
168             Flags&
169             operator = (const Flags& rhs)
170             {
171                 if (&rhs != this)
172                     m_flags = rhs.m_flags;
173                 
174                 return *this;
175             }
176             
177             Flags&
178             operator = (const uint32_t& rhs)
179             {
180                 m_flags = rhs;
181                 return *this;
182             }
183             
184             Flags&
185             Clear()
186             {
187                 m_flags = 0;
188                 return *this;
189             }
190             
191             bool
192             GetCascades () const
193             {
194                 return (m_flags & lldb::eTypeOptionCascade) == lldb::eTypeOptionCascade;
195             }
196             
197             Flags&
198             SetCascades (bool value = true)
199             {
200                 if (value)
201                     m_flags |= lldb::eTypeOptionCascade;
202                 else
203                     m_flags &= ~lldb::eTypeOptionCascade;
204                 return *this;
205             }
206             
207             bool
208             GetSkipPointers () const
209             {
210                 return (m_flags & lldb::eTypeOptionSkipPointers) == lldb::eTypeOptionSkipPointers;
211             }
212             
213             Flags&
214             SetSkipPointers (bool value = true)
215             {
216                 if (value)
217                     m_flags |= lldb::eTypeOptionSkipPointers;
218                 else
219                     m_flags &= ~lldb::eTypeOptionSkipPointers;
220                 return *this;
221             }
222             
223             bool
224             GetSkipReferences () const
225             {
226                 return (m_flags & lldb::eTypeOptionSkipReferences) == lldb::eTypeOptionSkipReferences;
227             }
228             
229             Flags&
230             SetSkipReferences (bool value = true)
231             {
232                 if (value)
233                     m_flags |= lldb::eTypeOptionSkipReferences;
234                 else
235                     m_flags &= ~lldb::eTypeOptionSkipReferences;
236                 return *this;
237             }
238             
239             bool
240             GetNonCacheable () const
241             {
242                 return (m_flags & lldb::eTypeOptionNonCacheable) == lldb::eTypeOptionNonCacheable;
243             }
244             
245             Flags&
246             SetNonCacheable (bool value = true)
247             {
248                 if (value)
249                     m_flags |= lldb::eTypeOptionNonCacheable;
250                 else
251                     m_flags &= ~lldb::eTypeOptionNonCacheable;
252                 return *this;
253             }
254             
255             uint32_t
256             GetValue ()
257             {
258                 return m_flags;
259             }
260             
261             void
262             SetValue (uint32_t value)
263             {
264                 m_flags = value;
265             }
266             
267         private:
268             uint32_t m_flags;
269         };
270         
271         SyntheticChildren (const Flags& flags) :
272         m_flags(flags)
273         {
274         }
275         
276         virtual
277         ~SyntheticChildren ()
278         {
279         }
280         
281         bool
282         Cascades () const
283         {
284             return m_flags.GetCascades();
285         }
286         bool
287         SkipsPointers () const
288         {
289             return m_flags.GetSkipPointers();
290         }
291         bool
292         SkipsReferences () const
293         {
294             return m_flags.GetSkipReferences();
295         }
296         bool
297         NonCacheable () const
298         {
299             return m_flags.GetNonCacheable();
300         }
301         
302         void
303         SetCascades (bool value)
304         {
305             m_flags.SetCascades(value);
306         }
307         
308         void
309         SetSkipsPointers (bool value)
310         {
311             m_flags.SetSkipPointers(value);
312         }
313         
314         void
315         SetSkipsReferences (bool value)
316         {
317             m_flags.SetSkipReferences(value);
318         }
319         
320         void
321         SetNonCacheable (bool value)
322         {
323             m_flags.SetNonCacheable(value);
324         }
325         
326         uint32_t
327         GetOptions ()
328         {
329             return m_flags.GetValue();
330         }
331         
332         void
333         SetOptions (uint32_t value)
334         {
335             m_flags.SetValue(value);
336         }
337         
338         virtual bool
339         IsScripted () = 0;
340         
341         virtual std::string
342         GetDescription () = 0;
343         
344         virtual SyntheticChildrenFrontEnd::AutoPointer
345         GetFrontEnd (ValueObject &backend) = 0;
346         
347         typedef std::shared_ptr<SyntheticChildren> SharedPointer;
348         typedef bool(*SyntheticChildrenCallback)(void*, ConstString, const SyntheticChildren::SharedPointer&);
349         
350         uint32_t&
351         GetRevision ()
352         {
353             return m_my_revision;
354         }
355         
356     protected:
357         uint32_t m_my_revision;
358         Flags m_flags;
359         
360     private:
361         DISALLOW_COPY_AND_ASSIGN(SyntheticChildren);
362     };
363     
364     class TypeFilterImpl : public SyntheticChildren
365     {
366         std::vector<std::string> m_expression_paths;
367     public:
368         TypeFilterImpl(const SyntheticChildren::Flags& flags) :
369         SyntheticChildren(flags),
370         m_expression_paths()
371         {
372         }
373
374         TypeFilterImpl(const SyntheticChildren::Flags& flags,
375                        const std::initializer_list<const char*> items) :
376         SyntheticChildren(flags),
377         m_expression_paths()
378         {
379             for (auto path : items)
380                 AddExpressionPath (path);
381         }
382         
383         void
384         AddExpressionPath (const char* path)
385         {
386             AddExpressionPath(std::string(path));
387         }
388         
389         void
390         Clear()
391         {
392             m_expression_paths.clear();
393         }
394         
395         size_t
396         GetCount() const
397         {
398             return m_expression_paths.size();
399         }
400         
401         const char*
402         GetExpressionPathAtIndex(size_t i) const
403         {
404             return m_expression_paths[i].c_str();
405         }
406         
407         bool
408         SetExpressionPathAtIndex (size_t i, const char* path)
409         {
410             return SetExpressionPathAtIndex(i, std::string(path));
411         }
412         
413         void
414         AddExpressionPath (const std::string& path);
415         
416         bool
417         SetExpressionPathAtIndex (size_t i, const std::string& path);
418         
419         bool
420         IsScripted ()
421         {
422             return false;
423         }
424         
425         std::string
426         GetDescription ();
427         
428         class FrontEnd : public SyntheticChildrenFrontEnd
429         {
430         private:
431             TypeFilterImpl* filter;
432         public:
433             
434             FrontEnd(TypeFilterImpl* flt,
435                      ValueObject &backend) :
436             SyntheticChildrenFrontEnd(backend),
437             filter(flt)
438             {}
439             
440             virtual
441             ~FrontEnd ()
442             {
443             }
444             
445             virtual size_t
446             CalculateNumChildren ()
447             {
448                 return filter->GetCount();
449             }
450             
451             virtual lldb::ValueObjectSP
452             GetChildAtIndex (size_t idx)
453             {
454                 if (idx >= filter->GetCount())
455                     return lldb::ValueObjectSP();
456                 return m_backend.GetSyntheticExpressionPathChild(filter->GetExpressionPathAtIndex(idx), true);
457             }
458             
459             virtual bool
460             Update() { return false; }
461             
462             virtual bool
463             MightHaveChildren ()
464             {
465                 return filter->GetCount() > 0;
466             }
467             
468             virtual size_t
469             GetIndexOfChildWithName (const ConstString &name);
470             
471             typedef std::shared_ptr<SyntheticChildrenFrontEnd> SharedPointer;
472             
473         private:
474             DISALLOW_COPY_AND_ASSIGN(FrontEnd);
475         };
476         
477         virtual SyntheticChildrenFrontEnd::AutoPointer
478         GetFrontEnd(ValueObject &backend)
479         {
480             return SyntheticChildrenFrontEnd::AutoPointer(new FrontEnd(this, backend));
481         }
482         
483     private:
484         DISALLOW_COPY_AND_ASSIGN(TypeFilterImpl);
485     };
486     
487     class CXXSyntheticChildren : public SyntheticChildren
488     {
489     public:
490         typedef SyntheticChildrenFrontEnd* (*CreateFrontEndCallback) (CXXSyntheticChildren*, lldb::ValueObjectSP);
491     protected:
492         CreateFrontEndCallback m_create_callback;
493         std::string m_description;
494     public:
495         CXXSyntheticChildren (const SyntheticChildren::Flags& flags,
496                               const char* description,
497                               CreateFrontEndCallback callback) :
498         SyntheticChildren(flags),
499         m_create_callback(callback),
500         m_description(description ? description : "")
501         {
502         }
503         
504         bool
505         IsScripted ()
506         {
507             return false;
508         }
509         
510         std::string
511         GetDescription ();
512         
513         virtual SyntheticChildrenFrontEnd::AutoPointer
514         GetFrontEnd (ValueObject &backend)
515         {
516             return SyntheticChildrenFrontEnd::AutoPointer(m_create_callback(this, backend.GetSP()));
517         }
518         
519     private:
520         DISALLOW_COPY_AND_ASSIGN(CXXSyntheticChildren);
521     };
522     
523 #ifndef LLDB_DISABLE_PYTHON
524     
525     class ScriptedSyntheticChildren : public SyntheticChildren
526     {
527         std::string m_python_class;
528         std::string m_python_code;
529     public:
530         
531         ScriptedSyntheticChildren (const SyntheticChildren::Flags& flags,
532                                    const char* pclass,
533                                    const char* pcode = NULL) :
534         SyntheticChildren(flags),
535         m_python_class(),
536         m_python_code()
537         {
538             if (pclass)
539                 m_python_class = pclass;
540             if (pcode)
541                 m_python_code = pcode;
542         }
543         
544         const char*
545         GetPythonClassName ()
546         {
547             return m_python_class.c_str();
548         }
549         
550         const char*
551         GetPythonCode ()
552         {
553             return m_python_code.c_str();
554         }
555         
556         void
557         SetPythonClassName (const char* fname)
558         {
559             m_python_class.assign(fname);
560             m_python_code.clear();
561         }
562         
563         void
564         SetPythonCode (const char* script)
565         {
566             m_python_code.assign(script);
567         }
568         
569         std::string
570         GetDescription ();
571         
572         bool
573         IsScripted ()
574         {
575             return true;
576         }
577         
578         class FrontEnd : public SyntheticChildrenFrontEnd
579         {
580         private:
581             std::string m_python_class;
582             StructuredData::ObjectSP m_wrapper_sp;
583             ScriptInterpreter *m_interpreter;
584         public:
585             
586             FrontEnd (std::string pclass,
587                       ValueObject &backend);
588             
589             bool
590             IsValid ();
591             
592             virtual
593             ~FrontEnd ();
594             
595             virtual size_t
596             CalculateNumChildren ();
597             
598             virtual lldb::ValueObjectSP
599             GetChildAtIndex (size_t idx);
600             
601             virtual bool
602             Update ();
603             
604             virtual bool
605             MightHaveChildren ();
606             
607             virtual size_t
608             GetIndexOfChildWithName (const ConstString &name);
609             
610             virtual lldb::ValueObjectSP
611             GetSyntheticValue ();
612             
613             typedef std::shared_ptr<SyntheticChildrenFrontEnd> SharedPointer;
614             
615         private:
616             DISALLOW_COPY_AND_ASSIGN(FrontEnd);
617         };
618         
619         virtual SyntheticChildrenFrontEnd::AutoPointer
620         GetFrontEnd(ValueObject &backend)
621         {
622             auto synth_ptr = SyntheticChildrenFrontEnd::AutoPointer(new FrontEnd(m_python_class, backend));
623             if (synth_ptr && ((FrontEnd*)synth_ptr.get())->IsValid())
624                 return synth_ptr;
625             return NULL;
626         }
627         
628     private:
629         DISALLOW_COPY_AND_ASSIGN(ScriptedSyntheticChildren);
630     };
631 #endif
632 } // namespace lldb_private
633
634 #endif  // lldb_TypeSynthetic_h_