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