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