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