TypeCategory.h revision 314564
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-enumerations.h"
24#include "lldb/lldb-public.h"
25
26#include "lldb/DataFormatters/FormatClasses.h"
27#include "lldb/DataFormatters/FormattersContainer.h"
28
29namespace lldb_private {
30
31template <typename FormatterImpl> class FormatterContainerPair {
32public:
33  typedef FormattersContainer<ConstString, FormatterImpl> ExactMatchContainer;
34  typedef FormattersContainer<lldb::RegularExpressionSP, FormatterImpl>
35      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
46      typename ExactMatchContainer::ForEachCallback ExactMatchForEachCallback;
47  typedef
48      typename RegexMatchContainer::ForEachCallback RegexMatchForEachCallback;
49
50  FormatterContainerPair(const char *exact_name, 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  ~FormatterContainerPair() = default;
56
57  ExactMatchContainerSP GetExactMatch() const { return m_exact_sp; }
58
59  RegexMatchContainerSP GetRegexMatch() const { return m_regex_sp; }
60
61  uint32_t GetCount() {
62    return GetExactMatch()->GetCount() + GetRegexMatch()->GetCount();
63  }
64
65private:
66  ExactMatchContainerSP m_exact_sp;
67  RegexMatchContainerSP m_regex_sp;
68};
69
70class TypeCategoryImpl {
71private:
72  typedef FormatterContainerPair<TypeFormatImpl> FormatContainer;
73  typedef FormatterContainerPair<TypeSummaryImpl> SummaryContainer;
74  typedef FormatterContainerPair<TypeFilterImpl> FilterContainer;
75  typedef FormatterContainerPair<TypeValidatorImpl> ValidatorContainer;
76
77#ifndef LLDB_DISABLE_PYTHON
78  typedef FormatterContainerPair<SyntheticChildren> SynthContainer;
79#endif // LLDB_DISABLE_PYTHON
80
81public:
82  typedef uint16_t FormatCategoryItems;
83  static const uint16_t ALL_ITEM_TYPES = UINT16_MAX;
84
85  typedef FormatContainer::ExactMatchContainerSP FormatContainerSP;
86  typedef FormatContainer::RegexMatchContainerSP RegexFormatContainerSP;
87
88  typedef SummaryContainer::ExactMatchContainerSP SummaryContainerSP;
89  typedef SummaryContainer::RegexMatchContainerSP RegexSummaryContainerSP;
90
91  typedef FilterContainer::ExactMatchContainerSP FilterContainerSP;
92  typedef FilterContainer::RegexMatchContainerSP RegexFilterContainerSP;
93#ifndef LLDB_DISABLE_PYTHON
94  typedef SynthContainer::ExactMatchContainerSP SynthContainerSP;
95  typedef SynthContainer::RegexMatchContainerSP RegexSynthContainerSP;
96#endif // LLDB_DISABLE_PYTHON
97
98  typedef ValidatorContainer::ExactMatchContainerSP ValidatorContainerSP;
99  typedef ValidatorContainer::RegexMatchContainerSP RegexValidatorContainerSP;
100
101  template <typename T> class ForEachCallbacks {
102  public:
103    ForEachCallbacks() = default;
104    ~ForEachCallbacks() = default;
105
106    template <typename U = TypeFormatImpl>
107    typename std::enable_if<std::is_same<U, T>::value, ForEachCallbacks &>::type
108    SetExact(FormatContainer::ExactMatchForEachCallback callback) {
109      m_format_exact = callback;
110      return *this;
111    }
112    template <typename U = TypeFormatImpl>
113    typename std::enable_if<std::is_same<U, T>::value, ForEachCallbacks &>::type
114    SetWithRegex(FormatContainer::RegexMatchForEachCallback callback) {
115      m_format_regex = callback;
116      return *this;
117    }
118
119    template <typename U = TypeSummaryImpl>
120    typename std::enable_if<std::is_same<U, T>::value, ForEachCallbacks &>::type
121    SetExact(SummaryContainer::ExactMatchForEachCallback callback) {
122      m_summary_exact = callback;
123      return *this;
124    }
125    template <typename U = TypeSummaryImpl>
126    typename std::enable_if<std::is_same<U, T>::value, ForEachCallbacks &>::type
127    SetWithRegex(SummaryContainer::RegexMatchForEachCallback callback) {
128      m_summary_regex = callback;
129      return *this;
130    }
131
132    template <typename U = TypeFilterImpl>
133    typename std::enable_if<std::is_same<U, T>::value, ForEachCallbacks &>::type
134    SetExact(FilterContainer::ExactMatchForEachCallback callback) {
135      m_filter_exact = callback;
136      return *this;
137    }
138    template <typename U = TypeFilterImpl>
139    typename std::enable_if<std::is_same<U, T>::value, ForEachCallbacks &>::type
140    SetWithRegex(FilterContainer::RegexMatchForEachCallback callback) {
141      m_filter_regex = callback;
142      return *this;
143    }
144
145#ifndef LLDB_DISABLE_PYTHON
146    template <typename U = SyntheticChildren>
147    typename std::enable_if<std::is_same<U, T>::value, ForEachCallbacks &>::type
148    SetExact(SynthContainer::ExactMatchForEachCallback callback) {
149      m_synth_exact = callback;
150      return *this;
151    }
152    template <typename U = SyntheticChildren>
153    typename std::enable_if<std::is_same<U, T>::value, ForEachCallbacks &>::type
154    SetWithRegex(SynthContainer::RegexMatchForEachCallback callback) {
155      m_synth_regex = callback;
156      return *this;
157    }
158#endif // LLDB_DISABLE_PYTHON
159    template <typename U = TypeValidatorImpl>
160    typename std::enable_if<std::is_same<U, T>::value, ForEachCallbacks &>::type
161    SetExact(ValidatorContainer::ExactMatchForEachCallback callback) {
162      m_validator_exact = callback;
163      return *this;
164    }
165    template <typename U = TypeValidatorImpl>
166    typename std::enable_if<std::is_same<U, T>::value, ForEachCallbacks &>::type
167    SetWithRegex(ValidatorContainer::RegexMatchForEachCallback callback) {
168      m_validator_regex = callback;
169      return *this;
170    }
171
172    FormatContainer::ExactMatchForEachCallback GetFormatExactCallback() const {
173      return m_format_exact;
174    }
175    FormatContainer::RegexMatchForEachCallback GetFormatRegexCallback() const {
176      return m_format_regex;
177    }
178
179    SummaryContainer::ExactMatchForEachCallback
180    GetSummaryExactCallback() const {
181      return m_summary_exact;
182    }
183    SummaryContainer::RegexMatchForEachCallback
184    GetSummaryRegexCallback() const {
185      return m_summary_regex;
186    }
187
188    FilterContainer::ExactMatchForEachCallback GetFilterExactCallback() const {
189      return m_filter_exact;
190    }
191    FilterContainer::RegexMatchForEachCallback GetFilterRegexCallback() const {
192      return m_filter_regex;
193    }
194
195#ifndef LLDB_DISABLE_PYTHON
196    SynthContainer::ExactMatchForEachCallback GetSynthExactCallback() const {
197      return m_synth_exact;
198    }
199    SynthContainer::RegexMatchForEachCallback GetSynthRegexCallback() const {
200      return m_synth_regex;
201    }
202#endif // LLDB_DISABLE_PYTHON
203
204    ValidatorContainer::ExactMatchForEachCallback
205    GetValidatorExactCallback() const {
206      return m_validator_exact;
207    }
208    ValidatorContainer::RegexMatchForEachCallback
209    GetValidatorRegexCallback() const {
210      return m_validator_regex;
211    }
212
213  private:
214    FormatContainer::ExactMatchForEachCallback m_format_exact;
215    FormatContainer::RegexMatchForEachCallback m_format_regex;
216
217    SummaryContainer::ExactMatchForEachCallback m_summary_exact;
218    SummaryContainer::RegexMatchForEachCallback m_summary_regex;
219
220    FilterContainer::ExactMatchForEachCallback m_filter_exact;
221    FilterContainer::RegexMatchForEachCallback m_filter_regex;
222
223#ifndef LLDB_DISABLE_PYTHON
224    SynthContainer::ExactMatchForEachCallback m_synth_exact;
225    SynthContainer::RegexMatchForEachCallback m_synth_regex;
226#endif // LLDB_DISABLE_PYTHON
227
228    ValidatorContainer::ExactMatchForEachCallback m_validator_exact;
229    ValidatorContainer::RegexMatchForEachCallback m_validator_regex;
230  };
231
232  TypeCategoryImpl(IFormatChangeListener *clist, ConstString name,
233                   std::initializer_list<lldb::LanguageType> langs = {});
234
235  template <typename T> void ForEach(const ForEachCallbacks<T> &foreach) {
236    GetTypeFormatsContainer()->ForEach(foreach.GetFormatExactCallback());
237    GetRegexTypeFormatsContainer()->ForEach(foreach.GetFormatRegexCallback());
238
239    GetTypeSummariesContainer()->ForEach(foreach.GetSummaryExactCallback());
240    GetRegexTypeSummariesContainer()->ForEach(
241        foreach.GetSummaryRegexCallback());
242
243    GetTypeFiltersContainer()->ForEach(foreach.GetFilterExactCallback());
244    GetRegexTypeFiltersContainer()->ForEach(foreach.GetFilterRegexCallback());
245
246#ifndef LLDB_DISABLE_PYTHON
247    GetTypeSyntheticsContainer()->ForEach(foreach.GetSynthExactCallback());
248    GetRegexTypeSyntheticsContainer()->ForEach(foreach.GetSynthRegexCallback());
249#endif // LLDB_DISABLE_PYTHON
250
251    GetTypeValidatorsContainer()->ForEach(foreach.GetValidatorExactCallback());
252    GetRegexTypeValidatorsContainer()->ForEach(
253        foreach.GetValidatorRegexCallback());
254  }
255
256  FormatContainerSP GetTypeFormatsContainer() {
257    return m_format_cont.GetExactMatch();
258  }
259
260  RegexFormatContainerSP GetRegexTypeFormatsContainer() {
261    return m_format_cont.GetRegexMatch();
262  }
263
264  FormatContainer &GetFormatContainer() { return m_format_cont; }
265
266  SummaryContainerSP GetTypeSummariesContainer() {
267    return m_summary_cont.GetExactMatch();
268  }
269
270  RegexSummaryContainerSP GetRegexTypeSummariesContainer() {
271    return m_summary_cont.GetRegexMatch();
272  }
273
274  SummaryContainer &GetSummaryContainer() { return m_summary_cont; }
275
276  FilterContainerSP GetTypeFiltersContainer() {
277    return m_filter_cont.GetExactMatch();
278  }
279
280  RegexFilterContainerSP GetRegexTypeFiltersContainer() {
281    return m_filter_cont.GetRegexMatch();
282  }
283
284  FilterContainer &GetFilterContainer() { return m_filter_cont; }
285
286  FormatContainer::MapValueType
287  GetFormatForType(lldb::TypeNameSpecifierImplSP type_sp);
288
289  SummaryContainer::MapValueType
290  GetSummaryForType(lldb::TypeNameSpecifierImplSP type_sp);
291
292  FilterContainer::MapValueType
293  GetFilterForType(lldb::TypeNameSpecifierImplSP type_sp);
294
295#ifndef LLDB_DISABLE_PYTHON
296  SynthContainer::MapValueType
297  GetSyntheticForType(lldb::TypeNameSpecifierImplSP type_sp);
298#endif
299
300  ValidatorContainer::MapValueType
301  GetValidatorForType(lldb::TypeNameSpecifierImplSP type_sp);
302
303  lldb::TypeNameSpecifierImplSP
304  GetTypeNameSpecifierForFormatAtIndex(size_t index);
305
306  lldb::TypeNameSpecifierImplSP
307  GetTypeNameSpecifierForSummaryAtIndex(size_t index);
308
309  FormatContainer::MapValueType GetFormatAtIndex(size_t index);
310
311  SummaryContainer::MapValueType GetSummaryAtIndex(size_t index);
312
313  FilterContainer::MapValueType GetFilterAtIndex(size_t index);
314
315  lldb::TypeNameSpecifierImplSP
316  GetTypeNameSpecifierForFilterAtIndex(size_t index);
317
318#ifndef LLDB_DISABLE_PYTHON
319  SynthContainerSP GetTypeSyntheticsContainer() {
320    return m_synth_cont.GetExactMatch();
321  }
322
323  RegexSynthContainerSP GetRegexTypeSyntheticsContainer() {
324    return m_synth_cont.GetRegexMatch();
325  }
326
327  SynthContainer &GetSyntheticsContainer() { return m_synth_cont; }
328
329  SynthContainer::MapValueType GetSyntheticAtIndex(size_t index);
330
331  lldb::TypeNameSpecifierImplSP
332  GetTypeNameSpecifierForSyntheticAtIndex(size_t index);
333#endif // LLDB_DISABLE_PYTHON
334
335  ValidatorContainerSP GetTypeValidatorsContainer() {
336    return m_validator_cont.GetExactMatch();
337  }
338
339  RegexValidatorContainerSP GetRegexTypeValidatorsContainer() {
340    return m_validator_cont.GetRegexMatch();
341  }
342
343  ValidatorContainer::MapValueType GetValidatorAtIndex(size_t index);
344
345  lldb::TypeNameSpecifierImplSP
346  GetTypeNameSpecifierForValidatorAtIndex(size_t index);
347
348  bool IsEnabled() const { return m_enabled; }
349
350  uint32_t GetEnabledPosition() {
351    if (m_enabled == false)
352      return UINT32_MAX;
353    else
354      return m_enabled_position;
355  }
356
357  bool Get(ValueObject &valobj, const FormattersMatchVector &candidates,
358           lldb::TypeFormatImplSP &entry, uint32_t *reason = nullptr);
359
360  bool Get(ValueObject &valobj, const FormattersMatchVector &candidates,
361           lldb::TypeSummaryImplSP &entry, uint32_t *reason = nullptr);
362
363  bool Get(ValueObject &valobj, const FormattersMatchVector &candidates,
364           lldb::SyntheticChildrenSP &entry, uint32_t *reason = nullptr);
365
366  bool Get(ValueObject &valobj, const FormattersMatchVector &candidates,
367           lldb::TypeValidatorImplSP &entry, uint32_t *reason = nullptr);
368
369  void Clear(FormatCategoryItems items = ALL_ITEM_TYPES);
370
371  bool Delete(ConstString name, FormatCategoryItems items = ALL_ITEM_TYPES);
372
373  uint32_t GetCount(FormatCategoryItems items = ALL_ITEM_TYPES);
374
375  const char *GetName() { return m_name.GetCString(); }
376
377  size_t GetNumLanguages();
378
379  lldb::LanguageType GetLanguageAtIndex(size_t idx);
380
381  void AddLanguage(lldb::LanguageType lang);
382
383  bool HasLanguage(lldb::LanguageType lang);
384
385  std::string GetDescription();
386
387  bool AnyMatches(ConstString type_name,
388                  FormatCategoryItems items = ALL_ITEM_TYPES,
389                  bool only_enabled = true,
390                  const char **matching_category = nullptr,
391                  FormatCategoryItems *matching_type = nullptr);
392
393  typedef std::shared_ptr<TypeCategoryImpl> SharedPointer;
394
395private:
396  FormatContainer m_format_cont;
397  SummaryContainer m_summary_cont;
398  FilterContainer m_filter_cont;
399#ifndef LLDB_DISABLE_PYTHON
400  SynthContainer m_synth_cont;
401#endif // LLDB_DISABLE_PYTHON
402  ValidatorContainer m_validator_cont;
403
404  bool m_enabled;
405
406  IFormatChangeListener *m_change_listener;
407
408  std::recursive_mutex m_mutex;
409
410  ConstString m_name;
411
412  std::vector<lldb::LanguageType> m_languages;
413
414  uint32_t m_enabled_position;
415
416  void Enable(bool value, uint32_t position);
417
418  void Disable() { Enable(false, UINT32_MAX); }
419
420  bool IsApplicable(ValueObject &valobj);
421
422  uint32_t GetLastEnabledPosition() { return m_enabled_position; }
423
424  void SetEnabledPosition(uint32_t p) { m_enabled_position = p; }
425
426  friend class FormatManager;
427  friend class LanguageCategory;
428  friend class TypeCategoryMap;
429
430  friend class FormattersContainer<ConstString, TypeFormatImpl>;
431  friend class FormattersContainer<lldb::RegularExpressionSP, TypeFormatImpl>;
432
433  friend class FormattersContainer<ConstString, TypeSummaryImpl>;
434  friend class FormattersContainer<lldb::RegularExpressionSP, TypeSummaryImpl>;
435
436  friend class FormattersContainer<ConstString, TypeFilterImpl>;
437  friend class FormattersContainer<lldb::RegularExpressionSP, TypeFilterImpl>;
438
439#ifndef LLDB_DISABLE_PYTHON
440  friend class FormattersContainer<ConstString, ScriptedSyntheticChildren>;
441  friend class FormattersContainer<lldb::RegularExpressionSP,
442                                   ScriptedSyntheticChildren>;
443#endif // LLDB_DISABLE_PYTHON
444
445  friend class FormattersContainer<ConstString, TypeValidatorImpl>;
446  friend class FormattersContainer<lldb::RegularExpressionSP,
447                                   TypeValidatorImpl>;
448};
449
450} // namespace lldb_private
451
452#endif // lldb_TypeCategory_h_
453