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