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 /// Explicit instantiations for the three types.
58 /// \{
59 template bool
60 LanguageCategory::Get<lldb::TypeFormatImplSP>(FormattersMatchData &,
61                                               lldb::TypeFormatImplSP &);
62 template bool
63 LanguageCategory::Get<lldb::TypeSummaryImplSP>(FormattersMatchData &,
64                                                lldb::TypeSummaryImplSP &);
65 template bool
66 LanguageCategory::Get<lldb::SyntheticChildrenSP>(FormattersMatchData &,
67                                                  lldb::SyntheticChildrenSP &);
68 /// \}
69 
70 template <>
71 auto &LanguageCategory::GetHardcodedFinder<lldb::TypeFormatImplSP>() {
72   return m_hardcoded_formats;
73 }
74 
75 template <>
76 auto &LanguageCategory::GetHardcodedFinder<lldb::TypeSummaryImplSP>() {
77   return m_hardcoded_summaries;
78 }
79 
80 template <>
81 auto &LanguageCategory::GetHardcodedFinder<lldb::SyntheticChildrenSP>() {
82   return m_hardcoded_synthetics;
83 }
84 
85 template <typename ImplSP>
86 bool LanguageCategory::GetHardcoded(FormatManager &fmt_mgr,
87                                     FormattersMatchData &match_data,
88                                     ImplSP &retval_sp) {
89   if (!IsEnabled())
90     return false;
91 
92   ValueObject &valobj(match_data.GetValueObject());
93   lldb::DynamicValueType use_dynamic(match_data.GetDynamicValueType());
94 
95   for (auto &candidate : GetHardcodedFinder<ImplSP>()) {
96     if (auto result = candidate(valobj, use_dynamic, fmt_mgr)) {
97       retval_sp = result;
98       break;
99     }
100   }
101   return (bool)retval_sp;
102 }
103 
104 /// Explicit instantiations for the three types.
105 /// \{
106 template bool LanguageCategory::GetHardcoded<lldb::TypeFormatImplSP>(
107     FormatManager &, FormattersMatchData &, lldb::TypeFormatImplSP &);
108 template bool LanguageCategory::GetHardcoded<lldb::TypeSummaryImplSP>(
109     FormatManager &, FormattersMatchData &, lldb::TypeSummaryImplSP &);
110 template bool LanguageCategory::GetHardcoded<lldb::SyntheticChildrenSP>(
111     FormatManager &, FormattersMatchData &, lldb::SyntheticChildrenSP &);
112 /// \}
113 
114 lldb::TypeCategoryImplSP LanguageCategory::GetCategory() const {
115   return m_category_sp;
116 }
117 
118 FormatCache &LanguageCategory::GetFormatCache() { return m_format_cache; }
119 
120 void LanguageCategory::Enable() {
121   if (m_category_sp)
122     m_category_sp->Enable(true, TypeCategoryMap::Default);
123   m_enabled = true;
124 }
125 
126 void LanguageCategory::Disable() {
127   if (m_category_sp)
128     m_category_sp->Disable();
129   m_enabled = false;
130 }
131 
132 bool LanguageCategory::IsEnabled() { return m_enabled; }
133