1//===-- FormattersHelpers.h --------------------------------------*- C++
2//-*-===//
3//
4// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
5// See https://llvm.org/LICENSE.txt for license information.
6// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7//
8//===----------------------------------------------------------------------===//
9
10#ifndef lldb_FormattersHelpers_h_
11#define lldb_FormattersHelpers_h_
12
13#include "lldb/lldb-enumerations.h"
14#include "lldb/lldb-forward.h"
15
16#include "lldb/DataFormatters/TypeCategory.h"
17#include "lldb/DataFormatters/TypeFormat.h"
18#include "lldb/DataFormatters/TypeSummary.h"
19#include "lldb/DataFormatters/TypeSynthetic.h"
20
21namespace lldb_private {
22namespace formatters {
23void AddFormat(TypeCategoryImpl::SharedPointer category_sp, lldb::Format format,
24               ConstString type_name, TypeFormatImpl::Flags flags,
25               bool regex = false);
26
27void AddSummary(TypeCategoryImpl::SharedPointer category_sp,
28                lldb::TypeSummaryImplSP summary_sp, ConstString type_name,
29                bool regex = false);
30
31void AddStringSummary(TypeCategoryImpl::SharedPointer category_sp,
32                      const char *string, ConstString type_name,
33                      TypeSummaryImpl::Flags flags, bool regex = false);
34
35void AddOneLineSummary(TypeCategoryImpl::SharedPointer category_sp,
36                       ConstString type_name, TypeSummaryImpl::Flags flags,
37                       bool regex = false);
38
39void AddCXXSummary(TypeCategoryImpl::SharedPointer category_sp,
40                   CXXFunctionSummaryFormat::Callback funct,
41                   const char *description, ConstString type_name,
42                   TypeSummaryImpl::Flags flags, bool regex = false);
43
44void AddCXXSynthetic(TypeCategoryImpl::SharedPointer category_sp,
45                     CXXSyntheticChildren::CreateFrontEndCallback generator,
46                     const char *description, ConstString type_name,
47                     ScriptedSyntheticChildren::Flags flags,
48                     bool regex = false);
49
50void AddFilter(TypeCategoryImpl::SharedPointer category_sp,
51               std::vector<std::string> children, const char *description,
52               ConstString type_name, ScriptedSyntheticChildren::Flags flags,
53               bool regex = false);
54
55size_t ExtractIndexFromString(const char *item_name);
56
57lldb::addr_t GetArrayAddressOrPointerValue(ValueObject &valobj);
58
59time_t GetOSXEpoch();
60
61struct InferiorSizedWord {
62
63  InferiorSizedWord(const InferiorSizedWord &word) : ptr_size(word.ptr_size) {
64    if (ptr_size == 4)
65      thirty_two = word.thirty_two;
66    else
67      sixty_four = word.sixty_four;
68  }
69
70  InferiorSizedWord operator=(const InferiorSizedWord &word) {
71    ptr_size = word.ptr_size;
72    if (ptr_size == 4)
73      thirty_two = word.thirty_two;
74    else
75      sixty_four = word.sixty_four;
76    return *this;
77  }
78
79  InferiorSizedWord(uint64_t val, Process &process)
80      : ptr_size(process.GetAddressByteSize()) {
81    if (ptr_size == 4)
82      thirty_two = (uint32_t)val;
83    else if (ptr_size == 8)
84      sixty_four = val;
85    else
86      assert(false && "new pointer size is unknown");
87  }
88
89  bool IsNegative() const {
90    if (ptr_size == 4)
91      return ((int32_t)thirty_two) < 0;
92    else
93      return ((int64_t)sixty_four) < 0;
94  }
95
96  bool IsZero() const {
97    if (ptr_size == 4)
98      return thirty_two == 0;
99    else
100      return sixty_four == 0;
101  }
102
103  static InferiorSizedWord GetMaximum(Process &process) {
104    if (process.GetAddressByteSize() == 4)
105      return InferiorSizedWord(UINT32_MAX, 4);
106    else
107      return InferiorSizedWord(UINT64_MAX, 8);
108  }
109
110  InferiorSizedWord operator>>(int rhs) const {
111    if (ptr_size == 4)
112      return InferiorSizedWord(thirty_two >> rhs, 4);
113    return InferiorSizedWord(sixty_four >> rhs, 8);
114  }
115
116  InferiorSizedWord operator<<(int rhs) const {
117    if (ptr_size == 4)
118      return InferiorSizedWord(thirty_two << rhs, 4);
119    return InferiorSizedWord(sixty_four << rhs, 8);
120  }
121
122  InferiorSizedWord operator&(const InferiorSizedWord &word) const {
123    if (ptr_size != word.ptr_size)
124      return InferiorSizedWord(0, ptr_size);
125    if (ptr_size == 4)
126      return InferiorSizedWord(thirty_two & word.thirty_two, 4);
127    return InferiorSizedWord(sixty_four & word.sixty_four, 8);
128  }
129
130  InferiorSizedWord operator&(int x) const {
131    if (ptr_size == 4)
132      return InferiorSizedWord(thirty_two & x, 4);
133    return InferiorSizedWord(sixty_four & x, 8);
134  }
135
136  size_t GetBitSize() const { return ptr_size << 3; }
137
138  size_t GetByteSize() const { return ptr_size; }
139
140  uint64_t GetValue() const {
141    if (ptr_size == 4)
142      return (uint64_t)thirty_two;
143    return sixty_four;
144  }
145
146  InferiorSizedWord SignExtend() const {
147    if (ptr_size == 4)
148      return InferiorSizedWord((int32_t)thirty_two, 4);
149    return InferiorSizedWord((int64_t)sixty_four, 8);
150  }
151
152  uint8_t *CopyToBuffer(uint8_t *buffer) const {
153    if (ptr_size == 4) {
154      memcpy(buffer, &thirty_two, 4);
155      return buffer + 4;
156    } else {
157      memcpy(buffer, &sixty_four, 8);
158      return buffer + 8;
159    }
160  }
161
162  DataExtractor
163  GetAsData(lldb::ByteOrder byte_order = lldb::eByteOrderInvalid) const {
164    if (ptr_size == 4)
165      return DataExtractor(&thirty_two, 4, byte_order, 4);
166    else
167      return DataExtractor(&sixty_four, 8, byte_order, 8);
168  }
169
170private:
171  InferiorSizedWord(uint64_t val, size_t psz) : ptr_size(psz) {
172    if (ptr_size == 4)
173      thirty_two = (uint32_t)val;
174    else
175      sixty_four = val;
176  }
177
178  size_t ptr_size;
179  union {
180    uint32_t thirty_two;
181    uint64_t sixty_four;
182  };
183};
184} // namespace formatters
185} // namespace lldb_private
186
187#endif // lldb_FormattersHelpers_h_
188