1 //===-- FormatClasses.h -----------------------------------------*- C++ -*-===//
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 #ifndef lldb_FormatClasses_h_
10 #define lldb_FormatClasses_h_
17 #include "lldb/DataFormatters/TypeFormat.h"
18 #include "lldb/DataFormatters/TypeSummary.h"
19 #include "lldb/DataFormatters/TypeSynthetic.h"
20 #include "lldb/DataFormatters/TypeValidator.h"
21 #include "lldb/Symbol/CompilerType.h"
22 #include "lldb/Symbol/Type.h"
23 #include "lldb/lldb-enumerations.h"
24 #include "lldb/lldb-public.h"
26 namespace lldb_private {
28 class HardcodedFormatters {
30 template <typename FormatterType>
31 using HardcodedFormatterFinder =
32 std::function<typename FormatterType::SharedPointer(
33 lldb_private::ValueObject &, lldb::DynamicValueType,
36 template <typename FormatterType>
37 using HardcodedFormatterFinders =
38 std::vector<HardcodedFormatterFinder<FormatterType>>;
40 typedef HardcodedFormatterFinders<TypeFormatImpl> HardcodedFormatFinder;
41 typedef HardcodedFormatterFinders<TypeSummaryImpl> HardcodedSummaryFinder;
42 typedef HardcodedFormatterFinders<SyntheticChildren> HardcodedSyntheticFinder;
43 typedef HardcodedFormatterFinders<TypeValidatorImpl> HardcodedValidatorFinder;
46 class FormattersMatchCandidate {
48 FormattersMatchCandidate(ConstString name, uint32_t reason, bool strip_ptr,
49 bool strip_ref, bool strip_tydef)
50 : m_type_name(name), m_reason(reason), m_stripped_pointer(strip_ptr),
51 m_stripped_reference(strip_ref), m_stripped_typedef(strip_tydef) {}
53 ~FormattersMatchCandidate() = default;
55 ConstString GetTypeName() const { return m_type_name; }
57 uint32_t GetReason() const { return m_reason; }
59 bool DidStripPointer() const { return m_stripped_pointer; }
61 bool DidStripReference() const { return m_stripped_reference; }
63 bool DidStripTypedef() const { return m_stripped_typedef; }
65 template <class Formatter>
66 bool IsMatch(const std::shared_ptr<Formatter> &formatter_sp) const {
69 if (formatter_sp->Cascades() == false && DidStripTypedef())
71 if (formatter_sp->SkipsPointers() && DidStripPointer())
73 if (formatter_sp->SkipsReferences() && DidStripReference())
79 ConstString m_type_name;
81 bool m_stripped_pointer;
82 bool m_stripped_reference;
83 bool m_stripped_typedef;
86 typedef std::vector<FormattersMatchCandidate> FormattersMatchVector;
87 typedef std::vector<lldb::LanguageType> CandidateLanguagesVector;
89 class FormattersMatchData {
91 FormattersMatchData(ValueObject &, lldb::DynamicValueType);
93 FormattersMatchVector GetMatchesVector();
95 ConstString GetTypeForCache();
97 CandidateLanguagesVector GetCandidateLanguages();
99 ValueObject &GetValueObject();
101 lldb::DynamicValueType GetDynamicValueType();
104 ValueObject &m_valobj;
105 lldb::DynamicValueType m_dynamic_value_type;
106 std::pair<FormattersMatchVector, bool> m_formatters_match_vector;
107 ConstString m_type_for_cache;
108 CandidateLanguagesVector m_candidate_languages;
111 class TypeNameSpecifierImpl {
113 TypeNameSpecifierImpl() : m_is_regex(false), m_type() {}
115 TypeNameSpecifierImpl(llvm::StringRef name, bool is_regex)
116 : m_is_regex(is_regex), m_type() {
117 m_type.m_type_name = name;
120 // if constructing with a given type, is_regex cannot be true since we are
121 // giving an exact type to match
122 TypeNameSpecifierImpl(lldb::TypeSP type) : m_is_regex(false), m_type() {
124 m_type.m_type_name = type->GetName().GetStringRef();
125 m_type.m_compiler_type = type->GetForwardCompilerType();
129 TypeNameSpecifierImpl(CompilerType type) : m_is_regex(false), m_type() {
130 if (type.IsValid()) {
131 m_type.m_type_name.assign(type.GetConstTypeName().GetCString());
132 m_type.m_compiler_type = type;
136 const char *GetName() {
137 if (m_type.m_type_name.size())
138 return m_type.m_type_name.c_str();
142 CompilerType GetCompilerType() {
143 if (m_type.m_compiler_type.IsValid())
144 return m_type.m_compiler_type;
145 return CompilerType();
148 bool IsRegex() { return m_is_regex; }
152 // TODO: Replace this with TypeAndOrName.
154 std::string m_type_name;
155 CompilerType m_compiler_type;
160 DISALLOW_COPY_AND_ASSIGN(TypeNameSpecifierImpl);
163 } // namespace lldb_private
165 #endif // lldb_FormatClasses_h_