FormatClasses.h revision 360784
1321936Shselasky//===-- FormatClasses.h -----------------------------------------*- C++ -*-===//
2321936Shselasky//
3321936Shselasky// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4321936Shselasky// See https://llvm.org/LICENSE.txt for license information.
5321936Shselasky// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6321936Shselasky//
7321936Shselasky//===----------------------------------------------------------------------===//
8321936Shselasky
9321936Shselasky#ifndef lldb_FormatClasses_h_
10321936Shselasky#define lldb_FormatClasses_h_
11321936Shselasky
12321936Shselasky#include <functional>
13321936Shselasky#include <memory>
14321936Shselasky#include <string>
15321936Shselasky#include <vector>
16321936Shselasky
17321936Shselasky#include "lldb/DataFormatters/TypeFormat.h"
18321936Shselasky#include "lldb/DataFormatters/TypeSummary.h"
19321936Shselasky#include "lldb/DataFormatters/TypeSynthetic.h"
20321936Shselasky#include "lldb/Symbol/CompilerType.h"
21321936Shselasky#include "lldb/Symbol/Type.h"
22321936Shselasky#include "lldb/lldb-enumerations.h"
23321936Shselasky#include "lldb/lldb-public.h"
24321936Shselasky
25321936Shselaskynamespace lldb_private {
26321936Shselasky
27321936Shselaskyclass HardcodedFormatters {
28321936Shselaskypublic:
29321936Shselasky  template <typename FormatterType>
30321936Shselasky  using HardcodedFormatterFinder =
31321936Shselasky      std::function<typename FormatterType::SharedPointer(
32321936Shselasky          lldb_private::ValueObject &, lldb::DynamicValueType,
33321936Shselasky          FormatManager &)>;
34321936Shselasky
35321936Shselasky  template <typename FormatterType>
36321936Shselasky  using HardcodedFormatterFinders =
37321936Shselasky      std::vector<HardcodedFormatterFinder<FormatterType>>;
38321936Shselasky
39321936Shselasky  typedef HardcodedFormatterFinders<TypeFormatImpl> HardcodedFormatFinder;
40321936Shselasky  typedef HardcodedFormatterFinders<TypeSummaryImpl> HardcodedSummaryFinder;
41321936Shselasky  typedef HardcodedFormatterFinders<SyntheticChildren> HardcodedSyntheticFinder;
42321936Shselasky};
43321936Shselasky
44321936Shselaskyclass FormattersMatchCandidate {
45321936Shselaskypublic:
46321936Shselasky  FormattersMatchCandidate(ConstString name, uint32_t reason, bool strip_ptr,
47321936Shselasky                           bool strip_ref, bool strip_tydef)
48321936Shselasky      : m_type_name(name), m_reason(reason), m_stripped_pointer(strip_ptr),
49321936Shselasky        m_stripped_reference(strip_ref), m_stripped_typedef(strip_tydef) {}
50321936Shselasky
51321936Shselasky  ~FormattersMatchCandidate() = default;
52321936Shselasky
53321936Shselasky  ConstString GetTypeName() const { return m_type_name; }
54321936Shselasky
55321936Shselasky  uint32_t GetReason() const { return m_reason; }
56321936Shselasky
57321936Shselasky  bool DidStripPointer() const { return m_stripped_pointer; }
58321936Shselasky
59321936Shselasky  bool DidStripReference() const { return m_stripped_reference; }
60321936Shselasky
61321936Shselasky  bool DidStripTypedef() const { return m_stripped_typedef; }
62321936Shselasky
63321936Shselasky  template <class Formatter>
64321936Shselasky  bool IsMatch(const std::shared_ptr<Formatter> &formatter_sp) const {
65321936Shselasky    if (!formatter_sp)
66321936Shselasky      return false;
67321936Shselasky    if (formatter_sp->Cascades() == false && DidStripTypedef())
68321936Shselasky      return false;
69321936Shselasky    if (formatter_sp->SkipsPointers() && DidStripPointer())
70321936Shselasky      return false;
71321936Shselasky    if (formatter_sp->SkipsReferences() && DidStripReference())
72321936Shselasky      return false;
73321936Shselasky    return true;
74321936Shselasky  }
75321936Shselasky
76321936Shselaskyprivate:
77321936Shselasky  ConstString m_type_name;
78321936Shselasky  uint32_t m_reason;
79321936Shselasky  bool m_stripped_pointer;
80321936Shselasky  bool m_stripped_reference;
81321936Shselasky  bool m_stripped_typedef;
82321936Shselasky};
83321936Shselasky
84321936Shselaskytypedef std::vector<FormattersMatchCandidate> FormattersMatchVector;
85321936Shselaskytypedef std::vector<lldb::LanguageType> CandidateLanguagesVector;
86321936Shselasky
87321936Shselaskyclass FormattersMatchData {
88321936Shselaskypublic:
89321936Shselasky  FormattersMatchData(ValueObject &, lldb::DynamicValueType);
90321936Shselasky
91321936Shselasky  FormattersMatchVector GetMatchesVector();
92321936Shselasky
93321936Shselasky  ConstString GetTypeForCache();
94321936Shselasky
95321936Shselasky  CandidateLanguagesVector GetCandidateLanguages();
96321936Shselasky
97321936Shselasky  ValueObject &GetValueObject();
98321936Shselasky
99321936Shselasky  lldb::DynamicValueType GetDynamicValueType();
100321936Shselasky
101321936Shselaskyprivate:
102321936Shselasky  ValueObject &m_valobj;
103321936Shselasky  lldb::DynamicValueType m_dynamic_value_type;
104321936Shselasky  std::pair<FormattersMatchVector, bool> m_formatters_match_vector;
105321936Shselasky  ConstString m_type_for_cache;
106321936Shselasky  CandidateLanguagesVector m_candidate_languages;
107321936Shselasky};
108321936Shselasky
109321936Shselaskyclass TypeNameSpecifierImpl {
110321936Shselaskypublic:
111321936Shselasky  TypeNameSpecifierImpl() : m_is_regex(false), m_type() {}
112321936Shselasky
113321936Shselasky  TypeNameSpecifierImpl(llvm::StringRef name, bool is_regex)
114321936Shselasky      : m_is_regex(is_regex), m_type() {
115321936Shselasky    m_type.m_type_name = name;
116321936Shselasky  }
117321936Shselasky
118321936Shselasky  // if constructing with a given type, is_regex cannot be true since we are
119321936Shselasky  // giving an exact type to match
120321936Shselasky  TypeNameSpecifierImpl(lldb::TypeSP type) : m_is_regex(false), m_type() {
121321936Shselasky    if (type) {
122321936Shselasky      m_type.m_type_name = type->GetName().GetStringRef();
123321936Shselasky      m_type.m_compiler_type = type->GetForwardCompilerType();
124321936Shselasky    }
125321936Shselasky  }
126321936Shselasky
127321936Shselasky  TypeNameSpecifierImpl(CompilerType type) : m_is_regex(false), m_type() {
128321936Shselasky    if (type.IsValid()) {
129321936Shselasky      m_type.m_type_name.assign(type.GetConstTypeName().GetCString());
130321936Shselasky      m_type.m_compiler_type = type;
131321936Shselasky    }
132321936Shselasky  }
133321936Shselasky
134321936Shselasky  const char *GetName() {
135321936Shselasky    if (m_type.m_type_name.size())
136321936Shselasky      return m_type.m_type_name.c_str();
137321936Shselasky    return nullptr;
138321936Shselasky  }
139321936Shselasky
140321936Shselasky  CompilerType GetCompilerType() {
141321936Shselasky    if (m_type.m_compiler_type.IsValid())
142321936Shselasky      return m_type.m_compiler_type;
143321936Shselasky    return CompilerType();
144321936Shselasky  }
145321936Shselasky
146321936Shselasky  bool IsRegex() { return m_is_regex; }
147321936Shselasky
148321936Shselaskyprivate:
149321936Shselasky  bool m_is_regex;
150321936Shselasky  // TODO: Replace this with TypeAndOrName.
151321936Shselasky  struct TypeOrName {
152321936Shselasky    std::string m_type_name;
153321936Shselasky    CompilerType m_compiler_type;
154321936Shselasky  };
155321936Shselasky  TypeOrName m_type;
156321936Shselasky
157321936Shselaskyprivate:
158321936Shselasky  DISALLOW_COPY_AND_ASSIGN(TypeNameSpecifierImpl);
159321936Shselasky};
160321936Shselasky
161321936Shselasky} // namespace lldb_private
162321936Shselasky
163321936Shselasky#endif // lldb_FormatClasses_h_
164321936Shselasky