1cfca06d7SDimitry Andric //===-- LanguageCategory.cpp ----------------------------------------------===//
2e81d9d49SDimitry Andric //
35f29bb8aSDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
45f29bb8aSDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
55f29bb8aSDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6e81d9d49SDimitry Andric //
7e81d9d49SDimitry Andric //===----------------------------------------------------------------------===//
8e81d9d49SDimitry Andric
9e81d9d49SDimitry Andric #include "lldb/DataFormatters/LanguageCategory.h"
10e81d9d49SDimitry Andric
11e81d9d49SDimitry Andric #include "lldb/DataFormatters/FormatManager.h"
12e81d9d49SDimitry Andric #include "lldb/DataFormatters/TypeCategory.h"
13e81d9d49SDimitry Andric #include "lldb/DataFormatters/TypeFormat.h"
14e81d9d49SDimitry Andric #include "lldb/DataFormatters/TypeSummary.h"
15e81d9d49SDimitry Andric #include "lldb/DataFormatters/TypeSynthetic.h"
16e81d9d49SDimitry Andric #include "lldb/Target/Language.h"
17e81d9d49SDimitry Andric
18e81d9d49SDimitry Andric using namespace lldb;
19e81d9d49SDimitry Andric using namespace lldb_private;
20e81d9d49SDimitry Andric
LanguageCategory(lldb::LanguageType lang_type)2114f1b3e8SDimitry Andric LanguageCategory::LanguageCategory(lldb::LanguageType lang_type)
2214f1b3e8SDimitry Andric : m_category_sp(), m_hardcoded_formats(), m_hardcoded_summaries(),
23706b4fc4SDimitry Andric m_hardcoded_synthetics(), m_format_cache(), m_enabled(false) {
2414f1b3e8SDimitry Andric if (Language *language_plugin = Language::FindPlugin(lang_type)) {
25e81d9d49SDimitry Andric m_category_sp = language_plugin->GetFormatters();
26e81d9d49SDimitry Andric m_hardcoded_formats = language_plugin->GetHardcodedFormats();
27e81d9d49SDimitry Andric m_hardcoded_summaries = language_plugin->GetHardcodedSummaries();
28e81d9d49SDimitry Andric m_hardcoded_synthetics = language_plugin->GetHardcodedSynthetics();
29e81d9d49SDimitry Andric }
30e81d9d49SDimitry Andric Enable();
31e81d9d49SDimitry Andric }
32e81d9d49SDimitry Andric
33706b4fc4SDimitry Andric template<typename ImplSP>
Get(FormattersMatchData & match_data,ImplSP & retval_sp)3414f1b3e8SDimitry Andric bool LanguageCategory::Get(FormattersMatchData &match_data,
35706b4fc4SDimitry Andric ImplSP &retval_sp) {
36e81d9d49SDimitry Andric if (!m_category_sp)
37e81d9d49SDimitry Andric return false;
38e81d9d49SDimitry Andric
39e81d9d49SDimitry Andric if (!IsEnabled())
40e81d9d49SDimitry Andric return false;
41e81d9d49SDimitry Andric
4214f1b3e8SDimitry Andric if (match_data.GetTypeForCache()) {
43706b4fc4SDimitry Andric if (m_format_cache.Get(match_data.GetTypeForCache(), retval_sp))
44706b4fc4SDimitry Andric return (bool)retval_sp;
45e81d9d49SDimitry Andric }
46e81d9d49SDimitry Andric
47e81d9d49SDimitry Andric ValueObject &valobj(match_data.GetValueObject());
48706b4fc4SDimitry Andric bool result = m_category_sp->Get(valobj.GetObjectRuntimeLanguage(),
49706b4fc4SDimitry Andric match_data.GetMatchesVector(), retval_sp);
5014f1b3e8SDimitry Andric if (match_data.GetTypeForCache() &&
51706b4fc4SDimitry Andric (!retval_sp || !retval_sp->NonCacheable())) {
52706b4fc4SDimitry Andric m_format_cache.Set(match_data.GetTypeForCache(), retval_sp);
53e81d9d49SDimitry Andric }
54e81d9d49SDimitry Andric return result;
55e81d9d49SDimitry Andric }
56e81d9d49SDimitry Andric
57cfca06d7SDimitry Andric namespace lldb_private {
58cfca06d7SDimitry Andric
59706b4fc4SDimitry Andric /// Explicit instantiations for the three types.
60706b4fc4SDimitry Andric /// \{
61706b4fc4SDimitry Andric template bool
62706b4fc4SDimitry Andric LanguageCategory::Get<lldb::TypeFormatImplSP>(FormattersMatchData &,
63706b4fc4SDimitry Andric lldb::TypeFormatImplSP &);
64706b4fc4SDimitry Andric template bool
65706b4fc4SDimitry Andric LanguageCategory::Get<lldb::TypeSummaryImplSP>(FormattersMatchData &,
66706b4fc4SDimitry Andric lldb::TypeSummaryImplSP &);
67706b4fc4SDimitry Andric template bool
68706b4fc4SDimitry Andric LanguageCategory::Get<lldb::SyntheticChildrenSP>(FormattersMatchData &,
69706b4fc4SDimitry Andric lldb::SyntheticChildrenSP &);
70706b4fc4SDimitry Andric /// \}
71e81d9d49SDimitry Andric
72706b4fc4SDimitry Andric template <>
GetHardcodedFinder()73706b4fc4SDimitry Andric auto &LanguageCategory::GetHardcodedFinder<lldb::TypeFormatImplSP>() {
74706b4fc4SDimitry Andric return m_hardcoded_formats;
75e81d9d49SDimitry Andric }
76e81d9d49SDimitry Andric
77706b4fc4SDimitry Andric template <>
GetHardcodedFinder()78706b4fc4SDimitry Andric auto &LanguageCategory::GetHardcodedFinder<lldb::TypeSummaryImplSP>() {
79706b4fc4SDimitry Andric return m_hardcoded_summaries;
80e81d9d49SDimitry Andric }
81e81d9d49SDimitry Andric
82706b4fc4SDimitry Andric template <>
GetHardcodedFinder()83706b4fc4SDimitry Andric auto &LanguageCategory::GetHardcodedFinder<lldb::SyntheticChildrenSP>() {
84706b4fc4SDimitry Andric return m_hardcoded_synthetics;
85e81d9d49SDimitry Andric }
86e81d9d49SDimitry Andric
87cfca06d7SDimitry Andric } // namespace lldb_private
88cfca06d7SDimitry Andric
89706b4fc4SDimitry Andric template <typename ImplSP>
GetHardcoded(FormatManager & fmt_mgr,FormattersMatchData & match_data,ImplSP & retval_sp)9014f1b3e8SDimitry Andric bool LanguageCategory::GetHardcoded(FormatManager &fmt_mgr,
91e81d9d49SDimitry Andric FormattersMatchData &match_data,
92706b4fc4SDimitry Andric ImplSP &retval_sp) {
93e81d9d49SDimitry Andric if (!IsEnabled())
94e81d9d49SDimitry Andric return false;
95e81d9d49SDimitry Andric
96e81d9d49SDimitry Andric ValueObject &valobj(match_data.GetValueObject());
97e81d9d49SDimitry Andric lldb::DynamicValueType use_dynamic(match_data.GetDynamicValueType());
98e81d9d49SDimitry Andric
99706b4fc4SDimitry Andric for (auto &candidate : GetHardcodedFinder<ImplSP>()) {
100706b4fc4SDimitry Andric if (auto result = candidate(valobj, use_dynamic, fmt_mgr)) {
101706b4fc4SDimitry Andric retval_sp = result;
102e81d9d49SDimitry Andric break;
103e81d9d49SDimitry Andric }
104e81d9d49SDimitry Andric }
105706b4fc4SDimitry Andric return (bool)retval_sp;
106e81d9d49SDimitry Andric }
107e81d9d49SDimitry Andric
108706b4fc4SDimitry Andric /// Explicit instantiations for the three types.
109706b4fc4SDimitry Andric /// \{
110706b4fc4SDimitry Andric template bool LanguageCategory::GetHardcoded<lldb::TypeFormatImplSP>(
111706b4fc4SDimitry Andric FormatManager &, FormattersMatchData &, lldb::TypeFormatImplSP &);
112706b4fc4SDimitry Andric template bool LanguageCategory::GetHardcoded<lldb::TypeSummaryImplSP>(
113706b4fc4SDimitry Andric FormatManager &, FormattersMatchData &, lldb::TypeSummaryImplSP &);
114706b4fc4SDimitry Andric template bool LanguageCategory::GetHardcoded<lldb::SyntheticChildrenSP>(
115706b4fc4SDimitry Andric FormatManager &, FormattersMatchData &, lldb::SyntheticChildrenSP &);
116706b4fc4SDimitry Andric /// \}
117e81d9d49SDimitry Andric
GetCategory() const11814f1b3e8SDimitry Andric lldb::TypeCategoryImplSP LanguageCategory::GetCategory() const {
119e81d9d49SDimitry Andric return m_category_sp;
120e81d9d49SDimitry Andric }
121e81d9d49SDimitry Andric
GetFormatCache()12214f1b3e8SDimitry Andric FormatCache &LanguageCategory::GetFormatCache() { return m_format_cache; }
123e81d9d49SDimitry Andric
Enable()12414f1b3e8SDimitry Andric void LanguageCategory::Enable() {
125e81d9d49SDimitry Andric if (m_category_sp)
126e81d9d49SDimitry Andric m_category_sp->Enable(true, TypeCategoryMap::Default);
127e81d9d49SDimitry Andric m_enabled = true;
128e81d9d49SDimitry Andric }
129e81d9d49SDimitry Andric
Disable()13014f1b3e8SDimitry Andric void LanguageCategory::Disable() {
131e81d9d49SDimitry Andric if (m_category_sp)
132e81d9d49SDimitry Andric m_category_sp->Disable();
133e81d9d49SDimitry Andric m_enabled = false;
134e81d9d49SDimitry Andric }
135e81d9d49SDimitry Andric
IsEnabled()13614f1b3e8SDimitry Andric bool LanguageCategory::IsEnabled() { return m_enabled; }
137