1 //===-- LanguageCategory.cpp ----------------------------------------------===//
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
7 //===----------------------------------------------------------------------===//
9 #include "lldb/DataFormatters/LanguageCategory.h"
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"
19 using namespace lldb_private
;
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();
33 template<typename ImplSP
>
34 bool LanguageCategory::Get(FormattersMatchData
&match_data
,
42 if (match_data
.GetTypeForCache()) {
43 if (m_format_cache
.Get(match_data
.GetTypeForCache(), retval_sp
))
44 return (bool)retval_sp
;
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
);
57 namespace lldb_private
{
59 /// Explicit instantiations for the three types.
62 LanguageCategory::Get
<lldb::TypeFormatImplSP
>(FormattersMatchData
&,
63 lldb::TypeFormatImplSP
&);
65 LanguageCategory::Get
<lldb::TypeSummaryImplSP
>(FormattersMatchData
&,
66 lldb::TypeSummaryImplSP
&);
68 LanguageCategory::Get
<lldb::SyntheticChildrenSP
>(FormattersMatchData
&,
69 lldb::SyntheticChildrenSP
&);
73 auto &LanguageCategory::GetHardcodedFinder
<lldb::TypeFormatImplSP
>() {
74 return m_hardcoded_formats
;
78 auto &LanguageCategory::GetHardcodedFinder
<lldb::TypeSummaryImplSP
>() {
79 return m_hardcoded_summaries
;
83 auto &LanguageCategory::GetHardcodedFinder
<lldb::SyntheticChildrenSP
>() {
84 return m_hardcoded_synthetics
;
87 } // namespace lldb_private
89 template <typename ImplSP
>
90 bool LanguageCategory::GetHardcoded(FormatManager
&fmt_mgr
,
91 FormattersMatchData
&match_data
,
96 ValueObject
&valobj(match_data
.GetValueObject());
97 lldb::DynamicValueType
use_dynamic(match_data
.GetDynamicValueType());
99 for (auto &candidate
: GetHardcodedFinder
<ImplSP
>()) {
100 if (auto result
= candidate(valobj
, use_dynamic
, fmt_mgr
)) {
105 return (bool)retval_sp
;
108 /// Explicit instantiations for the three types.
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
&);
118 lldb::TypeCategoryImplSP
LanguageCategory::GetCategory() const {
119 return m_category_sp
;
122 FormatCache
&LanguageCategory::GetFormatCache() { return m_format_cache
; }
124 void LanguageCategory::Enable() {
126 m_category_sp
->Enable(true, TypeCategoryMap::Default
);
130 void LanguageCategory::Disable() {
132 m_category_sp
->Disable();
136 bool LanguageCategory::IsEnabled() { return m_enabled
; }