FormatClasses.h revision 360784
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/Symbol/CompilerType.h"
21#include "lldb/Symbol/Type.h"
22#include "lldb/lldb-enumerations.h"
23#include "lldb/lldb-public.h"
24
25namespace lldb_private {
26
27class HardcodedFormatters {
28public:
29  template <typename FormatterType>
30  using HardcodedFormatterFinder =
31      std::function<typename FormatterType::SharedPointer(
32          lldb_private::ValueObject &, lldb::DynamicValueType,
33          FormatManager &)>;
34
35  template <typename FormatterType>
36  using HardcodedFormatterFinders =
37      std::vector<HardcodedFormatterFinder<FormatterType>>;
38
39  typedef HardcodedFormatterFinders<TypeFormatImpl> HardcodedFormatFinder;
40  typedef HardcodedFormatterFinders<TypeSummaryImpl> HardcodedSummaryFinder;
41  typedef HardcodedFormatterFinders<SyntheticChildren> HardcodedSyntheticFinder;
42};
43
44class FormattersMatchCandidate {
45public:
46  FormattersMatchCandidate(ConstString name, uint32_t reason, bool strip_ptr,
47                           bool strip_ref, bool strip_tydef)
48      : m_type_name(name), m_reason(reason), m_stripped_pointer(strip_ptr),
49        m_stripped_reference(strip_ref), m_stripped_typedef(strip_tydef) {}
50
51  ~FormattersMatchCandidate() = default;
52
53  ConstString GetTypeName() const { return m_type_name; }
54
55  uint32_t GetReason() const { return m_reason; }
56
57  bool DidStripPointer() const { return m_stripped_pointer; }
58
59  bool DidStripReference() const { return m_stripped_reference; }
60
61  bool DidStripTypedef() const { return m_stripped_typedef; }
62
63  template <class Formatter>
64  bool IsMatch(const std::shared_ptr<Formatter> &formatter_sp) const {
65    if (!formatter_sp)
66      return false;
67    if (formatter_sp->Cascades() == false && DidStripTypedef())
68      return false;
69    if (formatter_sp->SkipsPointers() && DidStripPointer())
70      return false;
71    if (formatter_sp->SkipsReferences() && DidStripReference())
72      return false;
73    return true;
74  }
75
76private:
77  ConstString m_type_name;
78  uint32_t m_reason;
79  bool m_stripped_pointer;
80  bool m_stripped_reference;
81  bool m_stripped_typedef;
82};
83
84typedef std::vector<FormattersMatchCandidate> FormattersMatchVector;
85typedef std::vector<lldb::LanguageType> CandidateLanguagesVector;
86
87class FormattersMatchData {
88public:
89  FormattersMatchData(ValueObject &, lldb::DynamicValueType);
90
91  FormattersMatchVector GetMatchesVector();
92
93  ConstString GetTypeForCache();
94
95  CandidateLanguagesVector GetCandidateLanguages();
96
97  ValueObject &GetValueObject();
98
99  lldb::DynamicValueType GetDynamicValueType();
100
101private:
102  ValueObject &m_valobj;
103  lldb::DynamicValueType m_dynamic_value_type;
104  std::pair<FormattersMatchVector, bool> m_formatters_match_vector;
105  ConstString m_type_for_cache;
106  CandidateLanguagesVector m_candidate_languages;
107};
108
109class TypeNameSpecifierImpl {
110public:
111  TypeNameSpecifierImpl() : m_is_regex(false), m_type() {}
112
113  TypeNameSpecifierImpl(llvm::StringRef name, bool is_regex)
114      : m_is_regex(is_regex), m_type() {
115    m_type.m_type_name = name;
116  }
117
118  // if constructing with a given type, is_regex cannot be true since we are
119  // giving an exact type to match
120  TypeNameSpecifierImpl(lldb::TypeSP type) : m_is_regex(false), m_type() {
121    if (type) {
122      m_type.m_type_name = type->GetName().GetStringRef();
123      m_type.m_compiler_type = type->GetForwardCompilerType();
124    }
125  }
126
127  TypeNameSpecifierImpl(CompilerType type) : m_is_regex(false), m_type() {
128    if (type.IsValid()) {
129      m_type.m_type_name.assign(type.GetConstTypeName().GetCString());
130      m_type.m_compiler_type = type;
131    }
132  }
133
134  const char *GetName() {
135    if (m_type.m_type_name.size())
136      return m_type.m_type_name.c_str();
137    return nullptr;
138  }
139
140  CompilerType GetCompilerType() {
141    if (m_type.m_compiler_type.IsValid())
142      return m_type.m_compiler_type;
143    return CompilerType();
144  }
145
146  bool IsRegex() { return m_is_regex; }
147
148private:
149  bool m_is_regex;
150  // TODO: Replace this with TypeAndOrName.
151  struct TypeOrName {
152    std::string m_type_name;
153    CompilerType m_compiler_type;
154  };
155  TypeOrName m_type;
156
157private:
158  DISALLOW_COPY_AND_ASSIGN(TypeNameSpecifierImpl);
159};
160
161} // namespace lldb_private
162
163#endif // lldb_FormatClasses_h_
164