TypeCategory.h revision 296417
1//===-- TypeCategory.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_TypeCategory_h_
11#define lldb_TypeCategory_h_
12
13// C Includes
14// C++ Includes
15#include <initializer_list>
16#include <memory>
17#include <string>
18#include <vector>
19
20// Other libraries and framework includes
21// Project includes
22#include "lldb/lldb-public.h"
23#include "lldb/lldb-enumerations.h"
24
25#include "lldb/DataFormatters/FormatClasses.h"
26#include "lldb/DataFormatters/FormattersContainer.h"
27
28namespace lldb_private {
29
30    template <typename FormatterImpl>
31    class FormatterContainerPair
32    {
33    public:
34        typedef FormattersContainer<ConstString, FormatterImpl> ExactMatchContainer;
35        typedef FormattersContainer<lldb::RegularExpressionSP, FormatterImpl> RegexMatchContainer;
36
37        typedef typename ExactMatchContainer::MapType ExactMatchMap;
38        typedef typename RegexMatchContainer::MapType RegexMatchMap;
39
40        typedef typename ExactMatchContainer::MapValueType MapValueType;
41
42        typedef typename ExactMatchContainer::SharedPointer ExactMatchContainerSP;
43        typedef typename RegexMatchContainer::SharedPointer RegexMatchContainerSP;
44
45        typedef typename ExactMatchContainer::ForEachCallback ExactMatchForEachCallback;
46        typedef typename RegexMatchContainer::ForEachCallback RegexMatchForEachCallback;
47
48        FormatterContainerPair (const char* exact_name,
49                                const char* regex_name,
50                                IFormatChangeListener* clist) :
51            m_exact_sp(new ExactMatchContainer(std::string(exact_name),clist)),
52            m_regex_sp(new RegexMatchContainer(std::string(regex_name),clist))
53        {
54        }
55
56        ~FormatterContainerPair () = default;
57
58        ExactMatchContainerSP
59        GetExactMatch () const
60        {
61            return m_exact_sp;
62        }
63
64        RegexMatchContainerSP
65        GetRegexMatch () const
66        {
67            return m_regex_sp;
68        }
69
70        uint32_t
71        GetCount ()
72        {
73            return GetExactMatch()->GetCount() + GetRegexMatch()->GetCount();
74        }
75
76    private:
77        ExactMatchContainerSP m_exact_sp;
78        RegexMatchContainerSP m_regex_sp;
79    };
80
81    class TypeCategoryImpl
82    {
83    private:
84        typedef FormatterContainerPair<TypeFormatImpl> FormatContainer;
85        typedef FormatterContainerPair<TypeSummaryImpl> SummaryContainer;
86        typedef FormatterContainerPair<TypeFilterImpl> FilterContainer;
87        typedef FormatterContainerPair<TypeValidatorImpl> ValidatorContainer;
88
89#ifndef LLDB_DISABLE_PYTHON
90        typedef FormatterContainerPair<SyntheticChildren> SynthContainer;
91#endif // LLDB_DISABLE_PYTHON
92
93    public:
94        typedef uint16_t FormatCategoryItems;
95        static const uint16_t ALL_ITEM_TYPES = UINT16_MAX;
96
97        typedef FormatContainer::ExactMatchContainerSP FormatContainerSP;
98        typedef FormatContainer::RegexMatchContainerSP RegexFormatContainerSP;
99
100        typedef SummaryContainer::ExactMatchContainerSP SummaryContainerSP;
101        typedef SummaryContainer::RegexMatchContainerSP RegexSummaryContainerSP;
102
103        typedef FilterContainer::ExactMatchContainerSP FilterContainerSP;
104        typedef FilterContainer::RegexMatchContainerSP RegexFilterContainerSP;
105#ifndef LLDB_DISABLE_PYTHON
106        typedef SynthContainer::ExactMatchContainerSP SynthContainerSP;
107        typedef SynthContainer::RegexMatchContainerSP RegexSynthContainerSP;
108#endif // LLDB_DISABLE_PYTHON
109
110        typedef ValidatorContainer::ExactMatchContainerSP ValidatorContainerSP;
111        typedef ValidatorContainer::RegexMatchContainerSP RegexValidatorContainerSP;
112
113        template <typename T>
114        class ForEachCallbacks
115        {
116        public:
117            ForEachCallbacks () = default;
118            ~ForEachCallbacks () = default;
119
120            template<typename U = TypeFormatImpl>
121            typename std::enable_if<std::is_same<U,T>::value, ForEachCallbacks&>::type
122            SetExact (FormatContainer::ExactMatchForEachCallback callback)
123            {
124                m_format_exact = callback;
125                return *this;
126            }
127            template<typename U = TypeFormatImpl>
128            typename std::enable_if<std::is_same<U,T>::value, ForEachCallbacks&>::type
129            SetWithRegex (FormatContainer::RegexMatchForEachCallback callback)
130            {
131                m_format_regex = callback;
132                return *this;
133            }
134
135            template<typename U = TypeSummaryImpl>
136            typename std::enable_if<std::is_same<U,T>::value, ForEachCallbacks&>::type
137            SetExact (SummaryContainer::ExactMatchForEachCallback callback)
138            {
139                m_summary_exact = callback;
140                return *this;
141            }
142            template<typename U = TypeSummaryImpl>
143            typename std::enable_if<std::is_same<U,T>::value, ForEachCallbacks&>::type
144            SetWithRegex (SummaryContainer::RegexMatchForEachCallback callback)
145            {
146                m_summary_regex = callback;
147                return *this;
148            }
149
150            template<typename U = TypeFilterImpl>
151            typename std::enable_if<std::is_same<U,T>::value, ForEachCallbacks&>::type
152            SetExact (FilterContainer::ExactMatchForEachCallback callback)
153            {
154                m_filter_exact = callback;
155                return *this;
156            }
157            template<typename U = TypeFilterImpl>
158            typename std::enable_if<std::is_same<U,T>::value, ForEachCallbacks&>::type
159            SetWithRegex (FilterContainer::RegexMatchForEachCallback callback)
160            {
161                m_filter_regex = callback;
162                return *this;
163            }
164
165#ifndef LLDB_DISABLE_PYTHON
166            template<typename U = SyntheticChildren>
167            typename std::enable_if<std::is_same<U,T>::value, ForEachCallbacks&>::type
168            SetExact (SynthContainer::ExactMatchForEachCallback callback)
169            {
170                m_synth_exact = callback;
171                return *this;
172            }
173            template<typename U = SyntheticChildren>
174            typename std::enable_if<std::is_same<U,T>::value, ForEachCallbacks&>::type
175            SetWithRegex (SynthContainer::RegexMatchForEachCallback callback)
176            {
177                m_synth_regex = callback;
178                return *this;
179            }
180#endif // LLDB_DISABLE_PYTHON
181            template<typename U = TypeValidatorImpl>
182            typename std::enable_if<std::is_same<U,T>::value, ForEachCallbacks&>::type
183            SetExact (ValidatorContainer::ExactMatchForEachCallback callback)
184            {
185                m_validator_exact = callback;
186                return *this;
187            }
188            template<typename U = TypeValidatorImpl>
189            typename std::enable_if<std::is_same<U,T>::value, ForEachCallbacks&>::type
190            SetWithRegex (ValidatorContainer::RegexMatchForEachCallback callback)
191            {
192                m_validator_regex = callback;
193                return *this;
194            }
195
196            FormatContainer::ExactMatchForEachCallback
197            GetFormatExactCallback () const
198            {
199                return m_format_exact;
200            }
201            FormatContainer::RegexMatchForEachCallback
202            GetFormatRegexCallback () const
203            {
204                return m_format_regex;
205            }
206
207            SummaryContainer::ExactMatchForEachCallback
208            GetSummaryExactCallback () const
209            {
210                return m_summary_exact;
211            }
212            SummaryContainer::RegexMatchForEachCallback
213            GetSummaryRegexCallback () const
214            {
215                return m_summary_regex;
216            }
217
218            FilterContainer::ExactMatchForEachCallback
219            GetFilterExactCallback () const
220            {
221                return m_filter_exact;
222            }
223            FilterContainer::RegexMatchForEachCallback
224            GetFilterRegexCallback () const
225            {
226                return m_filter_regex;
227            }
228
229#ifndef LLDB_DISABLE_PYTHON
230            SynthContainer::ExactMatchForEachCallback
231            GetSynthExactCallback () const
232            {
233                return m_synth_exact;
234            }
235            SynthContainer::RegexMatchForEachCallback
236            GetSynthRegexCallback () const
237            {
238                return m_synth_regex;
239            }
240#endif // LLDB_DISABLE_PYTHON
241
242            ValidatorContainer::ExactMatchForEachCallback
243            GetValidatorExactCallback () const
244            {
245                return m_validator_exact;
246            }
247            ValidatorContainer::RegexMatchForEachCallback
248            GetValidatorRegexCallback () const
249            {
250                return m_validator_regex;
251            }
252
253        private:
254            FormatContainer::ExactMatchForEachCallback m_format_exact;
255            FormatContainer::RegexMatchForEachCallback m_format_regex;
256
257            SummaryContainer::ExactMatchForEachCallback m_summary_exact;
258            SummaryContainer::RegexMatchForEachCallback m_summary_regex;
259
260            FilterContainer::ExactMatchForEachCallback m_filter_exact;
261            FilterContainer::RegexMatchForEachCallback m_filter_regex;
262
263#ifndef LLDB_DISABLE_PYTHON
264            SynthContainer::ExactMatchForEachCallback m_synth_exact;
265            SynthContainer::RegexMatchForEachCallback m_synth_regex;
266#endif // LLDB_DISABLE_PYTHON
267
268            ValidatorContainer::ExactMatchForEachCallback m_validator_exact;
269            ValidatorContainer::RegexMatchForEachCallback m_validator_regex;
270        };
271
272        TypeCategoryImpl (IFormatChangeListener* clist,
273                          ConstString name,
274                          std::initializer_list<lldb::LanguageType> langs = {});
275
276        template <typename T>
277        void
278        ForEach (const ForEachCallbacks<T> &foreach)
279        {
280            GetTypeFormatsContainer()->ForEach(foreach.GetFormatExactCallback());
281            GetRegexTypeFormatsContainer()->ForEach(foreach.GetFormatRegexCallback());
282
283            GetTypeSummariesContainer()->ForEach(foreach.GetSummaryExactCallback());
284            GetRegexTypeSummariesContainer()->ForEach(foreach.GetSummaryRegexCallback());
285
286            GetTypeFiltersContainer()->ForEach(foreach.GetFilterExactCallback());
287            GetRegexTypeFiltersContainer()->ForEach(foreach.GetFilterRegexCallback());
288
289#ifndef LLDB_DISABLE_PYTHON
290            GetTypeSyntheticsContainer()->ForEach(foreach.GetSynthExactCallback());
291            GetRegexTypeSyntheticsContainer()->ForEach(foreach.GetSynthRegexCallback());
292#endif // LLDB_DISABLE_PYTHON
293
294            GetTypeValidatorsContainer()->ForEach(foreach.GetValidatorExactCallback());
295            GetRegexTypeValidatorsContainer()->ForEach(foreach.GetValidatorRegexCallback());
296        }
297
298        FormatContainerSP
299        GetTypeFormatsContainer ()
300        {
301            return m_format_cont.GetExactMatch();
302        }
303
304        RegexFormatContainerSP
305        GetRegexTypeFormatsContainer ()
306        {
307            return m_format_cont.GetRegexMatch();
308        }
309
310        FormatContainer&
311        GetFormatContainer ()
312        {
313            return m_format_cont;
314        }
315
316        SummaryContainerSP
317        GetTypeSummariesContainer ()
318        {
319            return m_summary_cont.GetExactMatch();
320        }
321
322        RegexSummaryContainerSP
323        GetRegexTypeSummariesContainer ()
324        {
325            return m_summary_cont.GetRegexMatch();
326        }
327
328        SummaryContainer&
329        GetSummaryContainer ()
330        {
331            return m_summary_cont;
332        }
333
334        FilterContainerSP
335        GetTypeFiltersContainer ()
336        {
337            return m_filter_cont.GetExactMatch();
338        }
339
340        RegexFilterContainerSP
341        GetRegexTypeFiltersContainer ()
342        {
343            return m_filter_cont.GetRegexMatch();
344        }
345
346        FilterContainer&
347        GetFilterContainer ()
348        {
349            return m_filter_cont;
350        }
351
352        FormatContainer::MapValueType
353        GetFormatForType (lldb::TypeNameSpecifierImplSP type_sp);
354
355        SummaryContainer::MapValueType
356        GetSummaryForType (lldb::TypeNameSpecifierImplSP type_sp);
357
358        FilterContainer::MapValueType
359        GetFilterForType (lldb::TypeNameSpecifierImplSP type_sp);
360
361#ifndef LLDB_DISABLE_PYTHON
362        SynthContainer::MapValueType
363        GetSyntheticForType (lldb::TypeNameSpecifierImplSP type_sp);
364#endif
365
366        ValidatorContainer::MapValueType
367        GetValidatorForType (lldb::TypeNameSpecifierImplSP type_sp);
368
369        lldb::TypeNameSpecifierImplSP
370        GetTypeNameSpecifierForFormatAtIndex (size_t index);
371
372        lldb::TypeNameSpecifierImplSP
373        GetTypeNameSpecifierForSummaryAtIndex (size_t index);
374
375        FormatContainer::MapValueType
376        GetFormatAtIndex (size_t index);
377
378        SummaryContainer::MapValueType
379        GetSummaryAtIndex (size_t index);
380
381        FilterContainer::MapValueType
382        GetFilterAtIndex (size_t index);
383
384        lldb::TypeNameSpecifierImplSP
385        GetTypeNameSpecifierForFilterAtIndex (size_t index);
386
387#ifndef LLDB_DISABLE_PYTHON
388        SynthContainerSP
389        GetTypeSyntheticsContainer ()
390        {
391            return m_synth_cont.GetExactMatch();
392        }
393
394        RegexSynthContainerSP
395        GetRegexTypeSyntheticsContainer ()
396        {
397            return m_synth_cont.GetRegexMatch();
398        }
399
400        SynthContainer&
401        GetSyntheticsContainer ()
402        {
403            return m_synth_cont;
404        }
405
406        SynthContainer::MapValueType
407        GetSyntheticAtIndex (size_t index);
408
409        lldb::TypeNameSpecifierImplSP
410        GetTypeNameSpecifierForSyntheticAtIndex (size_t index);
411#endif // LLDB_DISABLE_PYTHON
412
413        ValidatorContainerSP
414        GetTypeValidatorsContainer ()
415        {
416            return m_validator_cont.GetExactMatch();
417        }
418
419        RegexValidatorContainerSP
420        GetRegexTypeValidatorsContainer ()
421        {
422            return m_validator_cont.GetRegexMatch();
423        }
424
425        ValidatorContainer::MapValueType
426        GetValidatorAtIndex (size_t index);
427
428        lldb::TypeNameSpecifierImplSP
429        GetTypeNameSpecifierForValidatorAtIndex (size_t index);
430
431        bool
432        IsEnabled () const
433        {
434            return m_enabled;
435        }
436
437        uint32_t
438        GetEnabledPosition()
439        {
440            if (m_enabled == false)
441                return UINT32_MAX;
442            else
443                return m_enabled_position;
444        }
445
446        bool
447        Get(ValueObject& valobj,
448            const FormattersMatchVector& candidates,
449            lldb::TypeFormatImplSP& entry,
450            uint32_t* reason = nullptr);
451
452        bool
453        Get(ValueObject& valobj,
454            const FormattersMatchVector& candidates,
455            lldb::TypeSummaryImplSP& entry,
456            uint32_t* reason = nullptr);
457
458        bool
459        Get(ValueObject& valobj,
460            const FormattersMatchVector& candidates,
461            lldb::SyntheticChildrenSP& entry,
462            uint32_t* reason = nullptr);
463
464        bool
465        Get(ValueObject& valobj,
466            const FormattersMatchVector& candidates,
467            lldb::TypeValidatorImplSP& entry,
468            uint32_t* reason = nullptr);
469
470        void
471        Clear (FormatCategoryItems items = ALL_ITEM_TYPES);
472
473        bool
474        Delete (ConstString name,
475                FormatCategoryItems items = ALL_ITEM_TYPES);
476
477        uint32_t
478        GetCount (FormatCategoryItems items = ALL_ITEM_TYPES);
479
480        const char*
481        GetName ()
482        {
483            return m_name.GetCString();
484        }
485
486        size_t
487        GetNumLanguages ();
488
489        lldb::LanguageType
490        GetLanguageAtIndex (size_t idx);
491
492        void
493        AddLanguage (lldb::LanguageType lang);
494
495        bool
496        HasLanguage (lldb::LanguageType lang);
497
498        std::string
499        GetDescription ();
500
501        bool
502        AnyMatches(ConstString type_name,
503                   FormatCategoryItems items = ALL_ITEM_TYPES,
504                   bool only_enabled = true,
505                   const char** matching_category = nullptr,
506                   FormatCategoryItems* matching_type = nullptr);
507
508        typedef std::shared_ptr<TypeCategoryImpl> SharedPointer;
509
510    private:
511        FormatContainer m_format_cont;
512        SummaryContainer m_summary_cont;
513        FilterContainer m_filter_cont;
514#ifndef LLDB_DISABLE_PYTHON
515        SynthContainer m_synth_cont;
516#endif // LLDB_DISABLE_PYTHON
517        ValidatorContainer m_validator_cont;
518
519        bool m_enabled;
520
521        IFormatChangeListener* m_change_listener;
522
523        Mutex m_mutex;
524
525        ConstString m_name;
526
527        std::vector<lldb::LanguageType> m_languages;
528
529        uint32_t m_enabled_position;
530
531        void
532        Enable (bool value, uint32_t position);
533
534        void
535        Disable ()
536        {
537            Enable(false, UINT32_MAX);
538        }
539
540        bool
541        IsApplicable (ValueObject& valobj);
542
543        uint32_t
544        GetLastEnabledPosition ()
545        {
546            return m_enabled_position;
547        }
548
549        void
550        SetEnabledPosition (uint32_t p)
551        {
552            m_enabled_position = p;
553        }
554
555        friend class FormatManager;
556        friend class LanguageCategory;
557        friend class TypeCategoryMap;
558
559        friend class FormattersContainer<ConstString, TypeFormatImpl>;
560        friend class FormattersContainer<lldb::RegularExpressionSP, TypeFormatImpl>;
561
562        friend class FormattersContainer<ConstString, TypeSummaryImpl>;
563        friend class FormattersContainer<lldb::RegularExpressionSP, TypeSummaryImpl>;
564
565        friend class FormattersContainer<ConstString, TypeFilterImpl>;
566        friend class FormattersContainer<lldb::RegularExpressionSP, TypeFilterImpl>;
567
568#ifndef LLDB_DISABLE_PYTHON
569        friend class FormattersContainer<ConstString, ScriptedSyntheticChildren>;
570        friend class FormattersContainer<lldb::RegularExpressionSP, ScriptedSyntheticChildren>;
571#endif // LLDB_DISABLE_PYTHON
572
573        friend class FormattersContainer<ConstString, TypeValidatorImpl>;
574        friend class FormattersContainer<lldb::RegularExpressionSP, TypeValidatorImpl>;
575    };
576
577} // namespace lldb_private
578
579#endif // lldb_TypeCategory_h_
580