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_
13 #include <initializer_list>
19 #include "lldb/lldb-enumerations.h"
20 #include "lldb/lldb-public.h"
22 #include "lldb/DataFormatters/FormatClasses.h"
23 #include "lldb/DataFormatters/FormattersContainer.h"
25 namespace lldb_private {
27 template <typename FormatterImpl> class FormatterContainerPair {
29 typedef FormattersContainer<ConstString, FormatterImpl> ExactMatchContainer;
30 typedef FormattersContainer<lldb::RegularExpressionSP, FormatterImpl>
33 typedef typename ExactMatchContainer::MapType ExactMatchMap;
34 typedef typename RegexMatchContainer::MapType RegexMatchMap;
36 typedef typename ExactMatchContainer::MapValueType MapValueType;
38 typedef typename ExactMatchContainer::SharedPointer ExactMatchContainerSP;
39 typedef typename RegexMatchContainer::SharedPointer RegexMatchContainerSP;
42 typename ExactMatchContainer::ForEachCallback ExactMatchForEachCallback;
44 typename RegexMatchContainer::ForEachCallback RegexMatchForEachCallback;
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)) {}
51 ~FormatterContainerPair() = default;
53 ExactMatchContainerSP GetExactMatch() const { return m_exact_sp; }
55 RegexMatchContainerSP GetRegexMatch() const { return m_regex_sp; }
58 return GetExactMatch()->GetCount() + GetRegexMatch()->GetCount();
62 ExactMatchContainerSP m_exact_sp;
63 RegexMatchContainerSP m_regex_sp;
66 class TypeCategoryImpl {
68 typedef FormatterContainerPair<TypeFormatImpl> FormatContainer;
69 typedef FormatterContainerPair<TypeSummaryImpl> SummaryContainer;
70 typedef FormatterContainerPair<TypeFilterImpl> FilterContainer;
71 typedef FormatterContainerPair<TypeValidatorImpl> ValidatorContainer;
73 #ifndef LLDB_DISABLE_PYTHON
74 typedef FormatterContainerPair<SyntheticChildren> SynthContainer;
75 #endif // LLDB_DISABLE_PYTHON
78 typedef uint16_t FormatCategoryItems;
79 static const uint16_t ALL_ITEM_TYPES = UINT16_MAX;
81 typedef FormatContainer::ExactMatchContainerSP FormatContainerSP;
82 typedef FormatContainer::RegexMatchContainerSP RegexFormatContainerSP;
84 typedef SummaryContainer::ExactMatchContainerSP SummaryContainerSP;
85 typedef SummaryContainer::RegexMatchContainerSP RegexSummaryContainerSP;
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
94 typedef ValidatorContainer::ExactMatchContainerSP ValidatorContainerSP;
95 typedef ValidatorContainer::RegexMatchContainerSP RegexValidatorContainerSP;
97 template <typename T> class ForEachCallbacks {
99 ForEachCallbacks() = default;
100 ~ForEachCallbacks() = default;
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;
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;
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;
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;
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;
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;
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;
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;
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;
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;
168 FormatContainer::ExactMatchForEachCallback GetFormatExactCallback() const {
169 return m_format_exact;
171 FormatContainer::RegexMatchForEachCallback GetFormatRegexCallback() const {
172 return m_format_regex;
175 SummaryContainer::ExactMatchForEachCallback
176 GetSummaryExactCallback() const {
177 return m_summary_exact;
179 SummaryContainer::RegexMatchForEachCallback
180 GetSummaryRegexCallback() const {
181 return m_summary_regex;
184 FilterContainer::ExactMatchForEachCallback GetFilterExactCallback() const {
185 return m_filter_exact;
187 FilterContainer::RegexMatchForEachCallback GetFilterRegexCallback() const {
188 return m_filter_regex;
191 #ifndef LLDB_DISABLE_PYTHON
192 SynthContainer::ExactMatchForEachCallback GetSynthExactCallback() const {
193 return m_synth_exact;
195 SynthContainer::RegexMatchForEachCallback GetSynthRegexCallback() const {
196 return m_synth_regex;
198 #endif // LLDB_DISABLE_PYTHON
200 ValidatorContainer::ExactMatchForEachCallback
201 GetValidatorExactCallback() const {
202 return m_validator_exact;
204 ValidatorContainer::RegexMatchForEachCallback
205 GetValidatorRegexCallback() const {
206 return m_validator_regex;
210 FormatContainer::ExactMatchForEachCallback m_format_exact;
211 FormatContainer::RegexMatchForEachCallback m_format_regex;
213 SummaryContainer::ExactMatchForEachCallback m_summary_exact;
214 SummaryContainer::RegexMatchForEachCallback m_summary_regex;
216 FilterContainer::ExactMatchForEachCallback m_filter_exact;
217 FilterContainer::RegexMatchForEachCallback m_filter_regex;
219 #ifndef LLDB_DISABLE_PYTHON
220 SynthContainer::ExactMatchForEachCallback m_synth_exact;
221 SynthContainer::RegexMatchForEachCallback m_synth_regex;
222 #endif // LLDB_DISABLE_PYTHON
224 ValidatorContainer::ExactMatchForEachCallback m_validator_exact;
225 ValidatorContainer::RegexMatchForEachCallback m_validator_regex;
228 TypeCategoryImpl(IFormatChangeListener *clist, ConstString name,
229 std::initializer_list<lldb::LanguageType> langs = {});
231 template <typename T> void ForEach(const ForEachCallbacks<T> &foreach) {
232 GetTypeFormatsContainer()->ForEach(foreach.GetFormatExactCallback());
233 GetRegexTypeFormatsContainer()->ForEach(foreach.GetFormatRegexCallback());
235 GetTypeSummariesContainer()->ForEach(foreach.GetSummaryExactCallback());
236 GetRegexTypeSummariesContainer()->ForEach(
237 foreach.GetSummaryRegexCallback());
239 GetTypeFiltersContainer()->ForEach(foreach.GetFilterExactCallback());
240 GetRegexTypeFiltersContainer()->ForEach(foreach.GetFilterRegexCallback());
242 #ifndef LLDB_DISABLE_PYTHON
243 GetTypeSyntheticsContainer()->ForEach(foreach.GetSynthExactCallback());
244 GetRegexTypeSyntheticsContainer()->ForEach(foreach.GetSynthRegexCallback());
245 #endif // LLDB_DISABLE_PYTHON
247 GetTypeValidatorsContainer()->ForEach(foreach.GetValidatorExactCallback());
248 GetRegexTypeValidatorsContainer()->ForEach(
249 foreach.GetValidatorRegexCallback());
252 FormatContainerSP GetTypeFormatsContainer() {
253 return m_format_cont.GetExactMatch();
256 RegexFormatContainerSP GetRegexTypeFormatsContainer() {
257 return m_format_cont.GetRegexMatch();
260 FormatContainer &GetFormatContainer() { return m_format_cont; }
262 SummaryContainerSP GetTypeSummariesContainer() {
263 return m_summary_cont.GetExactMatch();
266 RegexSummaryContainerSP GetRegexTypeSummariesContainer() {
267 return m_summary_cont.GetRegexMatch();
270 SummaryContainer &GetSummaryContainer() { return m_summary_cont; }
272 FilterContainerSP GetTypeFiltersContainer() {
273 return m_filter_cont.GetExactMatch();
276 RegexFilterContainerSP GetRegexTypeFiltersContainer() {
277 return m_filter_cont.GetRegexMatch();
280 FilterContainer &GetFilterContainer() { return m_filter_cont; }
282 FormatContainer::MapValueType
283 GetFormatForType(lldb::TypeNameSpecifierImplSP type_sp);
285 SummaryContainer::MapValueType
286 GetSummaryForType(lldb::TypeNameSpecifierImplSP type_sp);
288 FilterContainer::MapValueType
289 GetFilterForType(lldb::TypeNameSpecifierImplSP type_sp);
291 #ifndef LLDB_DISABLE_PYTHON
292 SynthContainer::MapValueType
293 GetSyntheticForType(lldb::TypeNameSpecifierImplSP type_sp);
296 ValidatorContainer::MapValueType
297 GetValidatorForType(lldb::TypeNameSpecifierImplSP type_sp);
299 lldb::TypeNameSpecifierImplSP
300 GetTypeNameSpecifierForFormatAtIndex(size_t index);
302 lldb::TypeNameSpecifierImplSP
303 GetTypeNameSpecifierForSummaryAtIndex(size_t index);
305 FormatContainer::MapValueType GetFormatAtIndex(size_t index);
307 SummaryContainer::MapValueType GetSummaryAtIndex(size_t index);
309 FilterContainer::MapValueType GetFilterAtIndex(size_t index);
311 lldb::TypeNameSpecifierImplSP
312 GetTypeNameSpecifierForFilterAtIndex(size_t index);
314 #ifndef LLDB_DISABLE_PYTHON
315 SynthContainerSP GetTypeSyntheticsContainer() {
316 return m_synth_cont.GetExactMatch();
319 RegexSynthContainerSP GetRegexTypeSyntheticsContainer() {
320 return m_synth_cont.GetRegexMatch();
323 SynthContainer &GetSyntheticsContainer() { return m_synth_cont; }
325 SynthContainer::MapValueType GetSyntheticAtIndex(size_t index);
327 lldb::TypeNameSpecifierImplSP
328 GetTypeNameSpecifierForSyntheticAtIndex(size_t index);
329 #endif // LLDB_DISABLE_PYTHON
331 ValidatorContainerSP GetTypeValidatorsContainer() {
332 return m_validator_cont.GetExactMatch();
335 RegexValidatorContainerSP GetRegexTypeValidatorsContainer() {
336 return m_validator_cont.GetRegexMatch();
339 ValidatorContainer::MapValueType GetValidatorAtIndex(size_t index);
341 lldb::TypeNameSpecifierImplSP
342 GetTypeNameSpecifierForValidatorAtIndex(size_t index);
344 bool IsEnabled() const { return m_enabled; }
346 uint32_t GetEnabledPosition() {
350 return m_enabled_position;
353 bool Get(ValueObject &valobj, const FormattersMatchVector &candidates,
354 lldb::TypeFormatImplSP &entry, uint32_t *reason = nullptr);
356 bool Get(ValueObject &valobj, const FormattersMatchVector &candidates,
357 lldb::TypeSummaryImplSP &entry, uint32_t *reason = nullptr);
359 bool Get(ValueObject &valobj, const FormattersMatchVector &candidates,
360 lldb::SyntheticChildrenSP &entry, uint32_t *reason = nullptr);
362 bool Get(ValueObject &valobj, const FormattersMatchVector &candidates,
363 lldb::TypeValidatorImplSP &entry, uint32_t *reason = nullptr);
365 void Clear(FormatCategoryItems items = ALL_ITEM_TYPES);
367 bool Delete(ConstString name, FormatCategoryItems items = ALL_ITEM_TYPES);
369 uint32_t GetCount(FormatCategoryItems items = ALL_ITEM_TYPES);
371 const char *GetName() { return m_name.GetCString(); }
373 size_t GetNumLanguages();
375 lldb::LanguageType GetLanguageAtIndex(size_t idx);
377 void AddLanguage(lldb::LanguageType lang);
379 bool HasLanguage(lldb::LanguageType lang);
381 std::string GetDescription();
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);
389 typedef std::shared_ptr<TypeCategoryImpl> SharedPointer;
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;
402 IFormatChangeListener *m_change_listener;
404 std::recursive_mutex m_mutex;
408 std::vector<lldb::LanguageType> m_languages;
410 uint32_t m_enabled_position;
412 void Enable(bool value, uint32_t position);
414 void Disable() { Enable(false, UINT32_MAX); }
416 bool IsApplicable(ValueObject &valobj);
418 uint32_t GetLastEnabledPosition() { return m_enabled_position; }
420 void SetEnabledPosition(uint32_t p) { m_enabled_position = p; }
422 friend class FormatManager;
423 friend class LanguageCategory;
424 friend class TypeCategoryMap;
426 friend class FormattersContainer<ConstString, TypeFormatImpl>;
427 friend class FormattersContainer<lldb::RegularExpressionSP, TypeFormatImpl>;
429 friend class FormattersContainer<ConstString, TypeSummaryImpl>;
430 friend class FormattersContainer<lldb::RegularExpressionSP, TypeSummaryImpl>;
432 friend class FormattersContainer<ConstString, TypeFilterImpl>;
433 friend class FormattersContainer<lldb::RegularExpressionSP, TypeFilterImpl>;
435 #ifndef LLDB_DISABLE_PYTHON
436 friend class FormattersContainer<ConstString, ScriptedSyntheticChildren>;
437 friend class FormattersContainer<lldb::RegularExpressionSP,
438 ScriptedSyntheticChildren>;
439 #endif // LLDB_DISABLE_PYTHON
441 friend class FormattersContainer<ConstString, TypeValidatorImpl>;
442 friend class FormattersContainer<lldb::RegularExpressionSP,
446 } // namespace lldb_private
448 #endif // lldb_TypeCategory_h_