xref: /freebsd-src/contrib/llvm-project/lldb/source/DataFormatters/LanguageCategory.cpp (revision 480093f4440d54b30b3025afeac24b48f2ba7a2e)
1 //===-- LanguageCategory.cpp ---------------------------------------*- C++
2 //-*-===//
3 //
4 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
5 // See https://llvm.org/LICENSE.txt for license information.
6 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //
8 //===----------------------------------------------------------------------===//
9 
10 #include "lldb/DataFormatters/LanguageCategory.h"
11 
12 #include "lldb/DataFormatters/FormatManager.h"
13 #include "lldb/DataFormatters/TypeCategory.h"
14 #include "lldb/DataFormatters/TypeFormat.h"
15 #include "lldb/DataFormatters/TypeSummary.h"
16 #include "lldb/DataFormatters/TypeSynthetic.h"
17 #include "lldb/Target/Language.h"
18 
19 using namespace lldb;
20 using namespace lldb_private;
21 
22 LanguageCategory::LanguageCategory(lldb::LanguageType lang_type)
23     : m_category_sp(), m_hardcoded_formats(), m_hardcoded_summaries(),
24       m_hardcoded_synthetics(), m_format_cache(), m_enabled(false) {
25   if (Language *language_plugin = Language::FindPlugin(lang_type)) {
26     m_category_sp = language_plugin->GetFormatters();
27     m_hardcoded_formats = language_plugin->GetHardcodedFormats();
28     m_hardcoded_summaries = language_plugin->GetHardcodedSummaries();
29     m_hardcoded_synthetics = language_plugin->GetHardcodedSynthetics();
30   }
31   Enable();
32 }
33 
34 template<typename ImplSP>
35 bool LanguageCategory::Get(FormattersMatchData &match_data,
36                            ImplSP &retval_sp) {
37   if (!m_category_sp)
38     return false;
39 
40   if (!IsEnabled())
41     return false;
42 
43   if (match_data.GetTypeForCache()) {
44     if (m_format_cache.Get(match_data.GetTypeForCache(), retval_sp))
45       return (bool)retval_sp;
46   }
47 
48   ValueObject &valobj(match_data.GetValueObject());
49   bool result = m_category_sp->Get(valobj.GetObjectRuntimeLanguage(),
50                                    match_data.GetMatchesVector(), retval_sp);
51   if (match_data.GetTypeForCache() &&
52       (!retval_sp || !retval_sp->NonCacheable())) {
53     m_format_cache.Set(match_data.GetTypeForCache(), retval_sp);
54   }
55   return result;
56 }
57 
58 /// Explicit instantiations for the three types.
59 /// \{
60 template bool
61 LanguageCategory::Get<lldb::TypeFormatImplSP>(FormattersMatchData &,
62                                               lldb::TypeFormatImplSP &);
63 template bool
64 LanguageCategory::Get<lldb::TypeSummaryImplSP>(FormattersMatchData &,
65                                                lldb::TypeSummaryImplSP &);
66 template bool
67 LanguageCategory::Get<lldb::SyntheticChildrenSP>(FormattersMatchData &,
68                                                  lldb::SyntheticChildrenSP &);
69 /// \}
70 
71 template <>
72 auto &LanguageCategory::GetHardcodedFinder<lldb::TypeFormatImplSP>() {
73   return m_hardcoded_formats;
74 }
75 
76 template <>
77 auto &LanguageCategory::GetHardcodedFinder<lldb::TypeSummaryImplSP>() {
78   return m_hardcoded_summaries;
79 }
80 
81 template <>
82 auto &LanguageCategory::GetHardcodedFinder<lldb::SyntheticChildrenSP>() {
83   return m_hardcoded_synthetics;
84 }
85 
86 template <typename ImplSP>
87 bool LanguageCategory::GetHardcoded(FormatManager &fmt_mgr,
88                                     FormattersMatchData &match_data,
89                                     ImplSP &retval_sp) {
90   if (!IsEnabled())
91     return false;
92 
93   ValueObject &valobj(match_data.GetValueObject());
94   lldb::DynamicValueType use_dynamic(match_data.GetDynamicValueType());
95 
96   for (auto &candidate : GetHardcodedFinder<ImplSP>()) {
97     if (auto result = candidate(valobj, use_dynamic, fmt_mgr)) {
98       retval_sp = result;
99       break;
100     }
101   }
102   return (bool)retval_sp;
103 }
104 
105 /// Explicit instantiations for the three types.
106 /// \{
107 template bool LanguageCategory::GetHardcoded<lldb::TypeFormatImplSP>(
108     FormatManager &, FormattersMatchData &, lldb::TypeFormatImplSP &);
109 template bool LanguageCategory::GetHardcoded<lldb::TypeSummaryImplSP>(
110     FormatManager &, FormattersMatchData &, lldb::TypeSummaryImplSP &);
111 template bool LanguageCategory::GetHardcoded<lldb::SyntheticChildrenSP>(
112     FormatManager &, FormattersMatchData &, lldb::SyntheticChildrenSP &);
113 /// \}
114 
115 lldb::TypeCategoryImplSP LanguageCategory::GetCategory() const {
116   return m_category_sp;
117 }
118 
119 FormatCache &LanguageCategory::GetFormatCache() { return m_format_cache; }
120 
121 void LanguageCategory::Enable() {
122   if (m_category_sp)
123     m_category_sp->Enable(true, TypeCategoryMap::Default);
124   m_enabled = true;
125 }
126 
127 void LanguageCategory::Disable() {
128   if (m_category_sp)
129     m_category_sp->Disable();
130   m_enabled = false;
131 }
132 
133 bool LanguageCategory::IsEnabled() { return m_enabled; }
134