1 //===-- TypeCategory.h ------------------------------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #ifndef lldb_TypeCategory_h_
11 #define lldb_TypeCategory_h_
15 #include <initializer_list>
21 // Other libraries and framework includes
23 #include "lldb/lldb-enumerations.h"
24 #include "lldb/lldb-public.h"
26 #include "lldb/DataFormatters/FormatClasses.h"
27 #include "lldb/DataFormatters/FormattersContainer.h"
29 namespace lldb_private {
31 template <typename FormatterImpl> class FormatterContainerPair {
33 typedef FormattersContainer<ConstString, FormatterImpl> ExactMatchContainer;
34 typedef FormattersContainer<lldb::RegularExpressionSP, FormatterImpl>
37 typedef typename ExactMatchContainer::MapType ExactMatchMap;
38 typedef typename RegexMatchContainer::MapType RegexMatchMap;
40 typedef typename ExactMatchContainer::MapValueType MapValueType;
42 typedef typename ExactMatchContainer::SharedPointer ExactMatchContainerSP;
43 typedef typename RegexMatchContainer::SharedPointer RegexMatchContainerSP;
46 typename ExactMatchContainer::ForEachCallback ExactMatchForEachCallback;
48 typename RegexMatchContainer::ForEachCallback RegexMatchForEachCallback;
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)) {}
55 ~FormatterContainerPair() = default;
57 ExactMatchContainerSP GetExactMatch() const { return m_exact_sp; }
59 RegexMatchContainerSP GetRegexMatch() const { return m_regex_sp; }
62 return GetExactMatch()->GetCount() + GetRegexMatch()->GetCount();
66 ExactMatchContainerSP m_exact_sp;
67 RegexMatchContainerSP m_regex_sp;
70 class TypeCategoryImpl {
72 typedef FormatterContainerPair<TypeFormatImpl> FormatContainer;
73 typedef FormatterContainerPair<TypeSummaryImpl> SummaryContainer;
74 typedef FormatterContainerPair<TypeFilterImpl> FilterContainer;
75 typedef FormatterContainerPair<TypeValidatorImpl> ValidatorContainer;
77 #ifndef LLDB_DISABLE_PYTHON
78 typedef FormatterContainerPair<SyntheticChildren> SynthContainer;
79 #endif // LLDB_DISABLE_PYTHON
82 typedef uint16_t FormatCategoryItems;
83 static const uint16_t ALL_ITEM_TYPES = UINT16_MAX;
85 typedef FormatContainer::ExactMatchContainerSP FormatContainerSP;
86 typedef FormatContainer::RegexMatchContainerSP RegexFormatContainerSP;
88 typedef SummaryContainer::ExactMatchContainerSP SummaryContainerSP;
89 typedef SummaryContainer::RegexMatchContainerSP RegexSummaryContainerSP;
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
98 typedef ValidatorContainer::ExactMatchContainerSP ValidatorContainerSP;
99 typedef ValidatorContainer::RegexMatchContainerSP RegexValidatorContainerSP;
101 template <typename T> class ForEachCallbacks {
103 ForEachCallbacks() = default;
104 ~ForEachCallbacks() = default;
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;
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;
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;
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;
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;
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;
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;
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;
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;
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;
172 FormatContainer::ExactMatchForEachCallback GetFormatExactCallback() const {
173 return m_format_exact;
175 FormatContainer::RegexMatchForEachCallback GetFormatRegexCallback() const {
176 return m_format_regex;
179 SummaryContainer::ExactMatchForEachCallback
180 GetSummaryExactCallback() const {
181 return m_summary_exact;
183 SummaryContainer::RegexMatchForEachCallback
184 GetSummaryRegexCallback() const {
185 return m_summary_regex;
188 FilterContainer::ExactMatchForEachCallback GetFilterExactCallback() const {
189 return m_filter_exact;
191 FilterContainer::RegexMatchForEachCallback GetFilterRegexCallback() const {
192 return m_filter_regex;
195 #ifndef LLDB_DISABLE_PYTHON
196 SynthContainer::ExactMatchForEachCallback GetSynthExactCallback() const {
197 return m_synth_exact;
199 SynthContainer::RegexMatchForEachCallback GetSynthRegexCallback() const {
200 return m_synth_regex;
202 #endif // LLDB_DISABLE_PYTHON
204 ValidatorContainer::ExactMatchForEachCallback
205 GetValidatorExactCallback() const {
206 return m_validator_exact;
208 ValidatorContainer::RegexMatchForEachCallback
209 GetValidatorRegexCallback() const {
210 return m_validator_regex;
214 FormatContainer::ExactMatchForEachCallback m_format_exact;
215 FormatContainer::RegexMatchForEachCallback m_format_regex;
217 SummaryContainer::ExactMatchForEachCallback m_summary_exact;
218 SummaryContainer::RegexMatchForEachCallback m_summary_regex;
220 FilterContainer::ExactMatchForEachCallback m_filter_exact;
221 FilterContainer::RegexMatchForEachCallback m_filter_regex;
223 #ifndef LLDB_DISABLE_PYTHON
224 SynthContainer::ExactMatchForEachCallback m_synth_exact;
225 SynthContainer::RegexMatchForEachCallback m_synth_regex;
226 #endif // LLDB_DISABLE_PYTHON
228 ValidatorContainer::ExactMatchForEachCallback m_validator_exact;
229 ValidatorContainer::RegexMatchForEachCallback m_validator_regex;
232 TypeCategoryImpl(IFormatChangeListener *clist, ConstString name,
233 std::initializer_list<lldb::LanguageType> langs = {});
235 template <typename T> void ForEach(const ForEachCallbacks<T> &foreach) {
236 GetTypeFormatsContainer()->ForEach(foreach.GetFormatExactCallback());
237 GetRegexTypeFormatsContainer()->ForEach(foreach.GetFormatRegexCallback());
239 GetTypeSummariesContainer()->ForEach(foreach.GetSummaryExactCallback());
240 GetRegexTypeSummariesContainer()->ForEach(
241 foreach.GetSummaryRegexCallback());
243 GetTypeFiltersContainer()->ForEach(foreach.GetFilterExactCallback());
244 GetRegexTypeFiltersContainer()->ForEach(foreach.GetFilterRegexCallback());
246 #ifndef LLDB_DISABLE_PYTHON
247 GetTypeSyntheticsContainer()->ForEach(foreach.GetSynthExactCallback());
248 GetRegexTypeSyntheticsContainer()->ForEach(foreach.GetSynthRegexCallback());
249 #endif // LLDB_DISABLE_PYTHON
251 GetTypeValidatorsContainer()->ForEach(foreach.GetValidatorExactCallback());
252 GetRegexTypeValidatorsContainer()->ForEach(
253 foreach.GetValidatorRegexCallback());
256 FormatContainerSP GetTypeFormatsContainer() {
257 return m_format_cont.GetExactMatch();
260 RegexFormatContainerSP GetRegexTypeFormatsContainer() {
261 return m_format_cont.GetRegexMatch();
264 FormatContainer &GetFormatContainer() { return m_format_cont; }
266 SummaryContainerSP GetTypeSummariesContainer() {
267 return m_summary_cont.GetExactMatch();
270 RegexSummaryContainerSP GetRegexTypeSummariesContainer() {
271 return m_summary_cont.GetRegexMatch();
274 SummaryContainer &GetSummaryContainer() { return m_summary_cont; }
276 FilterContainerSP GetTypeFiltersContainer() {
277 return m_filter_cont.GetExactMatch();
280 RegexFilterContainerSP GetRegexTypeFiltersContainer() {
281 return m_filter_cont.GetRegexMatch();
284 FilterContainer &GetFilterContainer() { return m_filter_cont; }
286 FormatContainer::MapValueType
287 GetFormatForType(lldb::TypeNameSpecifierImplSP type_sp);
289 SummaryContainer::MapValueType
290 GetSummaryForType(lldb::TypeNameSpecifierImplSP type_sp);
292 FilterContainer::MapValueType
293 GetFilterForType(lldb::TypeNameSpecifierImplSP type_sp);
295 #ifndef LLDB_DISABLE_PYTHON
296 SynthContainer::MapValueType
297 GetSyntheticForType(lldb::TypeNameSpecifierImplSP type_sp);
300 ValidatorContainer::MapValueType
301 GetValidatorForType(lldb::TypeNameSpecifierImplSP type_sp);
303 lldb::TypeNameSpecifierImplSP
304 GetTypeNameSpecifierForFormatAtIndex(size_t index);
306 lldb::TypeNameSpecifierImplSP
307 GetTypeNameSpecifierForSummaryAtIndex(size_t index);
309 FormatContainer::MapValueType GetFormatAtIndex(size_t index);
311 SummaryContainer::MapValueType GetSummaryAtIndex(size_t index);
313 FilterContainer::MapValueType GetFilterAtIndex(size_t index);
315 lldb::TypeNameSpecifierImplSP
316 GetTypeNameSpecifierForFilterAtIndex(size_t index);
318 #ifndef LLDB_DISABLE_PYTHON
319 SynthContainerSP GetTypeSyntheticsContainer() {
320 return m_synth_cont.GetExactMatch();
323 RegexSynthContainerSP GetRegexTypeSyntheticsContainer() {
324 return m_synth_cont.GetRegexMatch();
327 SynthContainer &GetSyntheticsContainer() { return m_synth_cont; }
329 SynthContainer::MapValueType GetSyntheticAtIndex(size_t index);
331 lldb::TypeNameSpecifierImplSP
332 GetTypeNameSpecifierForSyntheticAtIndex(size_t index);
333 #endif // LLDB_DISABLE_PYTHON
335 ValidatorContainerSP GetTypeValidatorsContainer() {
336 return m_validator_cont.GetExactMatch();
339 RegexValidatorContainerSP GetRegexTypeValidatorsContainer() {
340 return m_validator_cont.GetRegexMatch();
343 ValidatorContainer::MapValueType GetValidatorAtIndex(size_t index);
345 lldb::TypeNameSpecifierImplSP
346 GetTypeNameSpecifierForValidatorAtIndex(size_t index);
348 bool IsEnabled() const { return m_enabled; }
350 uint32_t GetEnabledPosition() {
351 if (m_enabled == false)
354 return m_enabled_position;
357 bool Get(ValueObject &valobj, const FormattersMatchVector &candidates,
358 lldb::TypeFormatImplSP &entry, uint32_t *reason = nullptr);
360 bool Get(ValueObject &valobj, const FormattersMatchVector &candidates,
361 lldb::TypeSummaryImplSP &entry, uint32_t *reason = nullptr);
363 bool Get(ValueObject &valobj, const FormattersMatchVector &candidates,
364 lldb::SyntheticChildrenSP &entry, uint32_t *reason = nullptr);
366 bool Get(ValueObject &valobj, const FormattersMatchVector &candidates,
367 lldb::TypeValidatorImplSP &entry, uint32_t *reason = nullptr);
369 void Clear(FormatCategoryItems items = ALL_ITEM_TYPES);
371 bool Delete(ConstString name, FormatCategoryItems items = ALL_ITEM_TYPES);
373 uint32_t GetCount(FormatCategoryItems items = ALL_ITEM_TYPES);
375 const char *GetName() { return m_name.GetCString(); }
377 size_t GetNumLanguages();
379 lldb::LanguageType GetLanguageAtIndex(size_t idx);
381 void AddLanguage(lldb::LanguageType lang);
383 bool HasLanguage(lldb::LanguageType lang);
385 std::string GetDescription();
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);
393 typedef std::shared_ptr<TypeCategoryImpl> SharedPointer;
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;
406 IFormatChangeListener *m_change_listener;
408 std::recursive_mutex m_mutex;
412 std::vector<lldb::LanguageType> m_languages;
414 uint32_t m_enabled_position;
416 void Enable(bool value, uint32_t position);
418 void Disable() { Enable(false, UINT32_MAX); }
420 bool IsApplicable(ValueObject &valobj);
422 uint32_t GetLastEnabledPosition() { return m_enabled_position; }
424 void SetEnabledPosition(uint32_t p) { m_enabled_position = p; }
426 friend class FormatManager;
427 friend class LanguageCategory;
428 friend class TypeCategoryMap;
430 friend class FormattersContainer<ConstString, TypeFormatImpl>;
431 friend class FormattersContainer<lldb::RegularExpressionSP, TypeFormatImpl>;
433 friend class FormattersContainer<ConstString, TypeSummaryImpl>;
434 friend class FormattersContainer<lldb::RegularExpressionSP, TypeSummaryImpl>;
436 friend class FormattersContainer<ConstString, TypeFilterImpl>;
437 friend class FormattersContainer<lldb::RegularExpressionSP, TypeFilterImpl>;
439 #ifndef LLDB_DISABLE_PYTHON
440 friend class FormattersContainer<ConstString, ScriptedSyntheticChildren>;
441 friend class FormattersContainer<lldb::RegularExpressionSP,
442 ScriptedSyntheticChildren>;
443 #endif // LLDB_DISABLE_PYTHON
445 friend class FormattersContainer<ConstString, TypeValidatorImpl>;
446 friend class FormattersContainer<lldb::RegularExpressionSP,
450 } // namespace lldb_private
452 #endif // lldb_TypeCategory_h_