]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/llvm-project/lldb/include/lldb/DataFormatters/FormatClasses.h
MFC r355940:
[FreeBSD/FreeBSD.git] / contrib / llvm-project / lldb / include / lldb / DataFormatters / FormatClasses.h
1 //===-- FormatClasses.h -----------------------------------------*- C++ -*-===//
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 #ifndef lldb_FormatClasses_h_
10 #define lldb_FormatClasses_h_
11
12 #include <functional>
13 #include <memory>
14 #include <string>
15 #include <vector>
16
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"
25
26 namespace lldb_private {
27
28 class HardcodedFormatters {
29 public:
30   template <typename FormatterType>
31   using HardcodedFormatterFinder =
32       std::function<typename FormatterType::SharedPointer(
33           lldb_private::ValueObject &, lldb::DynamicValueType,
34           FormatManager &)>;
35
36   template <typename FormatterType>
37   using HardcodedFormatterFinders =
38       std::vector<HardcodedFormatterFinder<FormatterType>>;
39
40   typedef HardcodedFormatterFinders<TypeFormatImpl> HardcodedFormatFinder;
41   typedef HardcodedFormatterFinders<TypeSummaryImpl> HardcodedSummaryFinder;
42   typedef HardcodedFormatterFinders<SyntheticChildren> HardcodedSyntheticFinder;
43   typedef HardcodedFormatterFinders<TypeValidatorImpl> HardcodedValidatorFinder;
44 };
45
46 class FormattersMatchCandidate {
47 public:
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) {}
52
53   ~FormattersMatchCandidate() = default;
54
55   ConstString GetTypeName() const { return m_type_name; }
56
57   uint32_t GetReason() const { return m_reason; }
58
59   bool DidStripPointer() const { return m_stripped_pointer; }
60
61   bool DidStripReference() const { return m_stripped_reference; }
62
63   bool DidStripTypedef() const { return m_stripped_typedef; }
64
65   template <class Formatter>
66   bool IsMatch(const std::shared_ptr<Formatter> &formatter_sp) const {
67     if (!formatter_sp)
68       return false;
69     if (formatter_sp->Cascades() == false && DidStripTypedef())
70       return false;
71     if (formatter_sp->SkipsPointers() && DidStripPointer())
72       return false;
73     if (formatter_sp->SkipsReferences() && DidStripReference())
74       return false;
75     return true;
76   }
77
78 private:
79   ConstString m_type_name;
80   uint32_t m_reason;
81   bool m_stripped_pointer;
82   bool m_stripped_reference;
83   bool m_stripped_typedef;
84 };
85
86 typedef std::vector<FormattersMatchCandidate> FormattersMatchVector;
87 typedef std::vector<lldb::LanguageType> CandidateLanguagesVector;
88
89 class FormattersMatchData {
90 public:
91   FormattersMatchData(ValueObject &, lldb::DynamicValueType);
92
93   FormattersMatchVector GetMatchesVector();
94
95   ConstString GetTypeForCache();
96
97   CandidateLanguagesVector GetCandidateLanguages();
98
99   ValueObject &GetValueObject();
100
101   lldb::DynamicValueType GetDynamicValueType();
102
103 private:
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;
109 };
110
111 class TypeNameSpecifierImpl {
112 public:
113   TypeNameSpecifierImpl() : m_is_regex(false), m_type() {}
114
115   TypeNameSpecifierImpl(llvm::StringRef name, bool is_regex)
116       : m_is_regex(is_regex), m_type() {
117     m_type.m_type_name = name;
118   }
119
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() {
123     if (type) {
124       m_type.m_type_name = type->GetName().GetStringRef();
125       m_type.m_compiler_type = type->GetForwardCompilerType();
126     }
127   }
128
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;
133     }
134   }
135
136   const char *GetName() {
137     if (m_type.m_type_name.size())
138       return m_type.m_type_name.c_str();
139     return nullptr;
140   }
141
142   CompilerType GetCompilerType() {
143     if (m_type.m_compiler_type.IsValid())
144       return m_type.m_compiler_type;
145     return CompilerType();
146   }
147
148   bool IsRegex() { return m_is_regex; }
149
150 private:
151   bool m_is_regex;
152   // TODO: Replace this with TypeAndOrName.
153   struct TypeOrName {
154     std::string m_type_name;
155     CompilerType m_compiler_type;
156   };
157   TypeOrName m_type;
158
159 private:
160   DISALLOW_COPY_AND_ASSIGN(TypeNameSpecifierImpl);
161 };
162
163 } // namespace lldb_private
164
165 #endif // lldb_FormatClasses_h_