aboutsummaryrefslogtreecommitdiffstats
path: root/include/lldb/DataFormatters/TypeCategory.h
blob: a5438226bbbb8764fbaae0ed9feaf7e4452abd49 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
//===-- TypeCategory.h ------------------------------------------*- C++ -*-===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

#ifndef lldb_TypeCategory_h_
#define lldb_TypeCategory_h_

#include <initializer_list>
#include <memory>
#include <mutex>
#include <string>
#include <vector>

#include "lldb/lldb-enumerations.h"
#include "lldb/lldb-public.h"

#include "lldb/DataFormatters/FormatClasses.h"
#include "lldb/DataFormatters/FormattersContainer.h"

namespace lldb_private {

template <typename FormatterImpl> class FormatterContainerPair {
public:
  typedef FormattersContainer<ConstString, FormatterImpl> ExactMatchContainer;
  typedef FormattersContainer<RegularExpression, FormatterImpl>
      RegexMatchContainer;

  typedef typename ExactMatchContainer::MapType ExactMatchMap;
  typedef typename RegexMatchContainer::MapType RegexMatchMap;

  typedef typename ExactMatchContainer::MapValueType MapValueType;

  typedef typename ExactMatchContainer::SharedPointer ExactMatchContainerSP;
  typedef typename RegexMatchContainer::SharedPointer RegexMatchContainerSP;

  typedef
      typename ExactMatchContainer::ForEachCallback ExactMatchForEachCallback;
  typedef
      typename RegexMatchContainer::ForEachCallback RegexMatchForEachCallback;

  FormatterContainerPair(const char *exact_name, const char *regex_name,
                         IFormatChangeListener *clist)
      : m_exact_sp(new ExactMatchContainer(std::string(exact_name), clist)),
        m_regex_sp(new RegexMatchContainer(std::string(regex_name), clist)) {}

  ~FormatterContainerPair() = default;

  ExactMatchContainerSP GetExactMatch() const { return m_exact_sp; }

  RegexMatchContainerSP GetRegexMatch() const { return m_regex_sp; }

  uint32_t GetCount() {
    return GetExactMatch()->GetCount() + GetRegexMatch()->GetCount();
  }

private:
  ExactMatchContainerSP m_exact_sp;
  RegexMatchContainerSP m_regex_sp;
};

class TypeCategoryImpl {
private:
  typedef FormatterContainerPair<TypeFormatImpl> FormatContainer;
  typedef FormatterContainerPair<TypeSummaryImpl> SummaryContainer;
  typedef FormatterContainerPair<TypeFilterImpl> FilterContainer;
  typedef FormatterContainerPair<TypeValidatorImpl> ValidatorContainer;
  typedef FormatterContainerPair<SyntheticChildren> SynthContainer;

public:
  typedef uint16_t FormatCategoryItems;
  static const uint16_t ALL_ITEM_TYPES = UINT16_MAX;

  typedef FormatContainer::ExactMatchContainerSP FormatContainerSP;
  typedef FormatContainer::RegexMatchContainerSP RegexFormatContainerSP;

  typedef SummaryContainer::ExactMatchContainerSP SummaryContainerSP;
  typedef SummaryContainer::RegexMatchContainerSP RegexSummaryContainerSP;

  typedef FilterContainer::ExactMatchContainerSP FilterContainerSP;
  typedef FilterContainer::RegexMatchContainerSP RegexFilterContainerSP;

  typedef SynthContainer::ExactMatchContainerSP SynthContainerSP;
  typedef SynthContainer::RegexMatchContainerSP RegexSynthContainerSP;

  typedef ValidatorContainer::ExactMatchContainerSP ValidatorContainerSP;
  typedef ValidatorContainer::RegexMatchContainerSP RegexValidatorContainerSP;

  template <typename T> class ForEachCallbacks {
  public:
    ForEachCallbacks() = default;
    ~ForEachCallbacks() = default;

    template <typename U = TypeFormatImpl>
    typename std::enable_if<std::is_same<U, T>::value, ForEachCallbacks &>::type
    SetExact(FormatContainer::ExactMatchForEachCallback callback) {
      m_format_exact = callback;
      return *this;
    }
    template <typename U = TypeFormatImpl>
    typename std::enable_if<std::is_same<U, T>::value, ForEachCallbacks &>::type
    SetWithRegex(FormatContainer::RegexMatchForEachCallback callback) {
      m_format_regex = callback;
      return *this;
    }

    template <typename U = TypeSummaryImpl>
    typename std::enable_if<std::is_same<U, T>::value, ForEachCallbacks &>::type
    SetExact(SummaryContainer::ExactMatchForEachCallback callback) {
      m_summary_exact = callback;
      return *this;
    }
    template <typename U = TypeSummaryImpl>
    typename std::enable_if<std::is_same<U, T>::value, ForEachCallbacks &>::type
    SetWithRegex(SummaryContainer::RegexMatchForEachCallback callback) {
      m_summary_regex = callback;
      return *this;
    }

    template <typename U = TypeFilterImpl>
    typename std::enable_if<std::is_same<U, T>::value, ForEachCallbacks &>::type
    SetExact(FilterContainer::ExactMatchForEachCallback callback) {
      m_filter_exact = callback;
      return *this;
    }
    template <typename U = TypeFilterImpl>
    typename std::enable_if<std::is_same<U, T>::value, ForEachCallbacks &>::type
    SetWithRegex(FilterContainer::RegexMatchForEachCallback callback) {
      m_filter_regex = callback;
      return *this;
    }

    template <typename U = SyntheticChildren>
    typename std::enable_if<std::is_same<U, T>::value, ForEachCallbacks &>::type
    SetExact(SynthContainer::ExactMatchForEachCallback callback) {
      m_synth_exact = callback;
      return *this;
    }
    template <typename U = SyntheticChildren>
    typename std::enable_if<std::is_same<U, T>::value, ForEachCallbacks &>::type
    SetWithRegex(SynthContainer::RegexMatchForEachCallback callback) {
      m_synth_regex = callback;
      return *this;
    }
    template <typename U = TypeValidatorImpl>
    typename std::enable_if<std::is_same<U, T>::value, ForEachCallbacks &>::type
    SetExact(ValidatorContainer::ExactMatchForEachCallback callback) {
      m_validator_exact = callback;
      return *this;
    }
    template <typename U = TypeValidatorImpl>
    typename std::enable_if<std::is_same<U, T>::value, ForEachCallbacks &>::type
    SetWithRegex(ValidatorContainer::RegexMatchForEachCallback callback) {
      m_validator_regex = callback;
      return *this;
    }

    FormatContainer::ExactMatchForEachCallback GetFormatExactCallback() const {
      return m_format_exact;
    }
    FormatContainer::RegexMatchForEachCallback GetFormatRegexCallback() const {
      return m_format_regex;
    }

    SummaryContainer::ExactMatchForEachCallback
    GetSummaryExactCallback() const {
      return m_summary_exact;
    }
    SummaryContainer::RegexMatchForEachCallback
    GetSummaryRegexCallback() const {
      return m_summary_regex;
    }

    FilterContainer::ExactMatchForEachCallback GetFilterExactCallback() const {
      return m_filter_exact;
    }
    FilterContainer::RegexMatchForEachCallback GetFilterRegexCallback() const {
      return m_filter_regex;
    }

    SynthContainer::ExactMatchForEachCallback GetSynthExactCallback() const {
      return m_synth_exact;
    }
    SynthContainer::RegexMatchForEachCallback GetSynthRegexCallback() const {
      return m_synth_regex;
    }

    ValidatorContainer::ExactMatchForEachCallback
    GetValidatorExactCallback() const {
      return m_validator_exact;
    }
    ValidatorContainer::RegexMatchForEachCallback
    GetValidatorRegexCallback() const {
      return m_validator_regex;
    }

  private:
    FormatContainer::ExactMatchForEachCallback m_format_exact;
    FormatContainer::RegexMatchForEachCallback m_format_regex;

    SummaryContainer::ExactMatchForEachCallback m_summary_exact;
    SummaryContainer::RegexMatchForEachCallback m_summary_regex;

    FilterContainer::ExactMatchForEachCallback m_filter_exact;
    FilterContainer::RegexMatchForEachCallback m_filter_regex;

    SynthContainer::ExactMatchForEachCallback m_synth_exact;
    SynthContainer::RegexMatchForEachCallback m_synth_regex;

    ValidatorContainer::ExactMatchForEachCallback m_validator_exact;
    ValidatorContainer::RegexMatchForEachCallback m_validator_regex;
  };

  TypeCategoryImpl(IFormatChangeListener *clist, ConstString name,
                   std::initializer_list<lldb::LanguageType> langs = {});

  template <typename T> void ForEach(const ForEachCallbacks<T> &foreach) {
    GetTypeFormatsContainer()->ForEach(foreach.GetFormatExactCallback());
    GetRegexTypeFormatsContainer()->ForEach(foreach.GetFormatRegexCallback());

    GetTypeSummariesContainer()->ForEach(foreach.GetSummaryExactCallback());
    GetRegexTypeSummariesContainer()->ForEach(
        foreach.GetSummaryRegexCallback());

    GetTypeFiltersContainer()->ForEach(foreach.GetFilterExactCallback());
    GetRegexTypeFiltersContainer()->ForEach(foreach.GetFilterRegexCallback());

    GetTypeSyntheticsContainer()->ForEach(foreach.GetSynthExactCallback());
    GetRegexTypeSyntheticsContainer()->ForEach(foreach.GetSynthRegexCallback());

    GetTypeValidatorsContainer()->ForEach(foreach.GetValidatorExactCallback());
    GetRegexTypeValidatorsContainer()->ForEach(
        foreach.GetValidatorRegexCallback());
  }

  FormatContainerSP GetTypeFormatsContainer() {
    return m_format_cont.GetExactMatch();
  }

  RegexFormatContainerSP GetRegexTypeFormatsContainer() {
    return m_format_cont.GetRegexMatch();
  }

  FormatContainer &GetFormatContainer() { return m_format_cont; }

  SummaryContainerSP GetTypeSummariesContainer() {
    return m_summary_cont.GetExactMatch();
  }

  RegexSummaryContainerSP GetRegexTypeSummariesContainer() {
    return m_summary_cont.GetRegexMatch();
  }

  SummaryContainer &GetSummaryContainer() { return m_summary_cont; }

  FilterContainerSP GetTypeFiltersContainer() {
    return m_filter_cont.GetExactMatch();
  }

  RegexFilterContainerSP GetRegexTypeFiltersContainer() {
    return m_filter_cont.GetRegexMatch();
  }

  FilterContainer &GetFilterContainer() { return m_filter_cont; }

  FormatContainer::MapValueType
  GetFormatForType(lldb::TypeNameSpecifierImplSP type_sp);

  SummaryContainer::MapValueType
  GetSummaryForType(lldb::TypeNameSpecifierImplSP type_sp);

  FilterContainer::MapValueType
  GetFilterForType(lldb::TypeNameSpecifierImplSP type_sp);

  SynthContainer::MapValueType
  GetSyntheticForType(lldb::TypeNameSpecifierImplSP type_sp);

  ValidatorContainer::MapValueType
  GetValidatorForType(lldb::TypeNameSpecifierImplSP type_sp);

  lldb::TypeNameSpecifierImplSP
  GetTypeNameSpecifierForFormatAtIndex(size_t index);

  lldb::TypeNameSpecifierImplSP
  GetTypeNameSpecifierForSummaryAtIndex(size_t index);

  FormatContainer::MapValueType GetFormatAtIndex(size_t index);

  SummaryContainer::MapValueType GetSummaryAtIndex(size_t index);

  FilterContainer::MapValueType GetFilterAtIndex(size_t index);

  lldb::TypeNameSpecifierImplSP
  GetTypeNameSpecifierForFilterAtIndex(size_t index);

  SynthContainerSP GetTypeSyntheticsContainer() {
    return m_synth_cont.GetExactMatch();
  }

  RegexSynthContainerSP GetRegexTypeSyntheticsContainer() {
    return m_synth_cont.GetRegexMatch();
  }

  SynthContainer &GetSyntheticsContainer() { return m_synth_cont; }

  SynthContainer::MapValueType GetSyntheticAtIndex(size_t index);

  lldb::TypeNameSpecifierImplSP
  GetTypeNameSpecifierForSyntheticAtIndex(size_t index);

  ValidatorContainerSP GetTypeValidatorsContainer() {
    return m_validator_cont.GetExactMatch();
  }

  RegexValidatorContainerSP GetRegexTypeValidatorsContainer() {
    return m_validator_cont.GetRegexMatch();
  }

  ValidatorContainer::MapValueType GetValidatorAtIndex(size_t index);

  lldb::TypeNameSpecifierImplSP
  GetTypeNameSpecifierForValidatorAtIndex(size_t index);

  bool IsEnabled() const { return m_enabled; }

  uint32_t GetEnabledPosition() {
    if (!m_enabled)
      return UINT32_MAX;
    else
      return m_enabled_position;
  }

  bool Get(ValueObject &valobj, const FormattersMatchVector &candidates,
           lldb::TypeFormatImplSP &entry, uint32_t *reason = nullptr);

  bool Get(ValueObject &valobj, const FormattersMatchVector &candidates,
           lldb::TypeSummaryImplSP &entry, uint32_t *reason = nullptr);

  bool Get(ValueObject &valobj, const FormattersMatchVector &candidates,
           lldb::SyntheticChildrenSP &entry, uint32_t *reason = nullptr);

  bool Get(ValueObject &valobj, const FormattersMatchVector &candidates,
           lldb::TypeValidatorImplSP &entry, uint32_t *reason = nullptr);

  void Clear(FormatCategoryItems items = ALL_ITEM_TYPES);

  bool Delete(ConstString name, FormatCategoryItems items = ALL_ITEM_TYPES);

  uint32_t GetCount(FormatCategoryItems items = ALL_ITEM_TYPES);

  const char *GetName() { return m_name.GetCString(); }

  size_t GetNumLanguages();

  lldb::LanguageType GetLanguageAtIndex(size_t idx);

  void AddLanguage(lldb::LanguageType lang);

  bool HasLanguage(lldb::LanguageType lang);

  std::string GetDescription();

  bool AnyMatches(ConstString type_name,
                  FormatCategoryItems items = ALL_ITEM_TYPES,
                  bool only_enabled = true,
                  const char **matching_category = nullptr,
                  FormatCategoryItems *matching_type = nullptr);

  typedef std::shared_ptr<TypeCategoryImpl> SharedPointer;

private:
  FormatContainer m_format_cont;
  SummaryContainer m_summary_cont;
  FilterContainer m_filter_cont;
  SynthContainer m_synth_cont;
  ValidatorContainer m_validator_cont;

  bool m_enabled;

  IFormatChangeListener *m_change_listener;

  std::recursive_mutex m_mutex;

  ConstString m_name;

  std::vector<lldb::LanguageType> m_languages;

  uint32_t m_enabled_position;

  void Enable(bool value, uint32_t position);

  void Disable() { Enable(false, UINT32_MAX); }

  bool IsApplicable(ValueObject &valobj);

  uint32_t GetLastEnabledPosition() { return m_enabled_position; }

  void SetEnabledPosition(uint32_t p) { m_enabled_position = p; }

  friend class FormatManager;
  friend class LanguageCategory;
  friend class TypeCategoryMap;

  friend class FormattersContainer<ConstString, TypeFormatImpl>;
  friend class FormattersContainer<lldb::RegularExpressionSP, TypeFormatImpl>;

  friend class FormattersContainer<ConstString, TypeSummaryImpl>;
  friend class FormattersContainer<lldb::RegularExpressionSP, TypeSummaryImpl>;

  friend class FormattersContainer<ConstString, TypeFilterImpl>;
  friend class FormattersContainer<lldb::RegularExpressionSP, TypeFilterImpl>;

  friend class FormattersContainer<ConstString, ScriptedSyntheticChildren>;
  friend class FormattersContainer<lldb::RegularExpressionSP,
                                   ScriptedSyntheticChildren>;

  friend class FormattersContainer<ConstString, TypeValidatorImpl>;
  friend class FormattersContainer<lldb::RegularExpressionSP,
                                   TypeValidatorImpl>;
};

} // namespace lldb_private

#endif // lldb_TypeCategory_h_