LLDB mainline
VectorType.cpp
Go to the documentation of this file.
1//===-- VectorType.cpp ----------------------------------------------------===//
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
10
15#include "lldb/Target/Target.h"
16
18#include "lldb/Utility/Log.h"
19#include <optional>
20
21using namespace lldb;
22using namespace lldb_private;
23using namespace lldb_private::formatters;
24
26 CompilerType element_type,
27 TypeSystemSP type_system) {
28 lldbassert(type_system && "type_system needs to be not NULL");
29 if (!type_system)
30 return {};
31
32 switch (format) {
35 return type_system->GetBuiltinTypeForEncodingAndBitSize(
36 eEncodingUint, 8 * type_system->GetPointerByteSize());
37
39 return type_system->GetBasicTypeFromAST(lldb::eBasicTypeBool);
40
46 return type_system->GetBasicTypeFromAST(lldb::eBasicTypeChar);
47
48 case lldb::eFormatComplex /* lldb::eFormatComplexFloat */:
49 return type_system->GetBasicTypeFromAST(lldb::eBasicTypeFloatComplex);
50
52 return type_system->GetBasicTypeFromAST(lldb::eBasicTypeChar)
53 .GetPointerType();
54
56 return type_system->GetBasicTypeFromAST(lldb::eBasicTypeFloat);
57
61 return type_system->GetBasicTypeFromAST(lldb::eBasicTypeInt);
62
64 return type_system->GetBasicTypeFromAST(lldb::eBasicTypeFloat);
65
68
70 return type_system->GetBasicTypeFromAST(lldb::eBasicTypeUnsignedInt);
71
73 return type_system->GetBasicTypeFromAST(lldb::eBasicTypeChar);
74
76 return type_system->GetBuiltinTypeForEncodingAndBitSize(eEncodingIEEE754,
77 32);
78
80 return type_system->GetBuiltinTypeForEncodingAndBitSize(eEncodingIEEE754,
81 64);
82
84 return type_system->GetBuiltinTypeForEncodingAndBitSize(eEncodingSint, 16);
85
87 return type_system->GetBuiltinTypeForEncodingAndBitSize(eEncodingSint, 32);
88
90 return type_system->GetBuiltinTypeForEncodingAndBitSize(eEncodingSint, 64);
91
93 return type_system->GetBuiltinTypeForEncodingAndBitSize(eEncodingSint, 8);
94
96 return type_system->GetBuiltinTypeForEncodingAndBitSize(eEncodingUint, 128);
97
99 return type_system->GetBuiltinTypeForEncodingAndBitSize(eEncodingUint, 16);
100
102 return type_system->GetBuiltinTypeForEncodingAndBitSize(eEncodingUint, 32);
103
105 return type_system->GetBuiltinTypeForEncodingAndBitSize(eEncodingUint, 64);
106
108 return type_system->GetBuiltinTypeForEncodingAndBitSize(eEncodingUint, 8);
109
111 return element_type;
112
120 default:
121 return type_system->GetBuiltinTypeForEncodingAndBitSize(eEncodingUint, 8);
122 }
123}
124
126 CompilerType element_type) {
127 switch (format) {
129 return lldb::eFormatChar;
130
133 return lldb::eFormatFloat;
134
140
147
155 return eFormatHex;
156
158 // special case the (default, char) combination to actually display as an
159 // integer value most often, you won't want to see the ASCII characters...
160 // (and if you do, eFormatChar is a keystroke away)
161 bool is_char = element_type.IsCharType();
162 bool is_signed = false;
163 element_type.IsIntegerType(is_signed);
164 return is_char ? (is_signed ? lldb::eFormatDecimal : eFormatHex) : format;
165 } break;
166
167 default:
168 return format;
169 }
170}
171
173 CompilerType container_type, CompilerType element_type,
175 nullptr // does not matter here because all we trade in are basic types
176 ) {
177 std::optional<uint64_t> container_size =
178 container_type.GetByteSize(exe_scope);
179 std::optional<uint64_t> element_size = element_type.GetByteSize(exe_scope);
180
181 if (container_size && element_size && *element_size) {
182 if (*container_size % *element_size)
183 return 0;
184 return *container_size / *element_size;
185 }
186 return 0;
187}
188
189namespace lldb_private {
190namespace formatters {
191
193public:
194 VectorTypeSyntheticFrontEnd(lldb::ValueObjectSP valobj_sp)
195 : SyntheticChildrenFrontEnd(*valobj_sp), m_child_type() {}
196
197 ~VectorTypeSyntheticFrontEnd() override = default;
198
199 size_t CalculateNumChildren() override { return m_num_children; }
200
201 lldb::ValueObjectSP GetChildAtIndex(size_t idx) override {
202 if (idx >= CalculateNumChildren())
203 return {};
204 std::optional<uint64_t> size = m_child_type.GetByteSize(nullptr);
205 if (!size)
206 return {};
207 auto offset = idx * *size;
208 StreamString idx_name;
209 idx_name.Printf("[%" PRIu64 "]", (uint64_t)idx);
210 ValueObjectSP child_sp(m_backend.GetSyntheticChildAtOffset(
211 offset, m_child_type, true, ConstString(idx_name.GetString())));
212 if (!child_sp)
213 return child_sp;
214
215 child_sp->SetFormat(m_item_format);
216
217 return child_sp;
218 }
219
220 bool Update() override {
223 CompilerType element_type;
224 parent_type.IsVectorType(&element_type);
226 m_parent_format, element_type,
227 parent_type.GetTypeSystem().GetSharedPointer());
230 return false;
231 }
232
233 bool MightHaveChildren() override { return true; }
234
235 size_t GetIndexOfChildWithName(ConstString name) override {
236 const char *item_name = name.GetCString();
237 uint32_t idx = ExtractIndexFromString(item_name);
238 if (idx < UINT32_MAX && idx >= CalculateNumChildren())
239 return UINT32_MAX;
240 return idx;
241 }
242
243private:
247 size_t m_num_children = 0;
248};
249
250} // namespace formatters
251} // namespace lldb_private
252
254 ValueObject &valobj, Stream &s, const TypeSummaryOptions &) {
255 auto synthetic_children =
256 VectorTypeSyntheticFrontEndCreator(nullptr, valobj.GetSP());
257 if (!synthetic_children)
258 return false;
259
260 synthetic_children->Update();
261
262 s.PutChar('(');
263 bool first = true;
264
265 size_t idx = 0, len = synthetic_children->CalculateNumChildren();
266
267 for (; idx < len; idx++) {
268 auto child_sp = synthetic_children->GetChildAtIndex(idx);
269 if (!child_sp)
270 continue;
271 child_sp = child_sp->GetQualifiedRepresentationIfAvailable(
273
274 const char *child_value = child_sp->GetValueAsCString();
275 if (child_value && *child_value) {
276 if (first) {
277 s.Printf("%s", child_value);
278 first = false;
279 } else {
280 s.Printf(", %s", child_value);
281 }
282 }
283 }
284
285 s.PutChar(')');
286
287 return true;
288}
289
292 CXXSyntheticChildren *, lldb::ValueObjectSP valobj_sp) {
293 if (!valobj_sp)
294 return nullptr;
295 return new VectorTypeSyntheticFrontEnd(valobj_sp);
296}
#define lldbassert(x)
Definition: LLDBAssert.h:13
static lldb::Format GetItemFormatForFormat(lldb::Format format, CompilerType element_type)
Definition: VectorType.cpp:125
static CompilerType GetCompilerTypeForFormat(lldb::Format format, CompilerType element_type, TypeSystemSP type_system)
Definition: VectorType.cpp:25
static size_t CalculateNumChildren(CompilerType container_type, CompilerType element_type, lldb_private::ExecutionContextScope *exe_scope=nullptr)
Definition: VectorType.cpp:172
lldb::TypeSystemSP GetSharedPointer() const
Definition: CompilerType.h:88
Generic representation of a type in a programming language.
Definition: CompilerType.h:36
TypeSystemSPWrapper GetTypeSystem() const
Accessors.
std::optional< uint64_t > GetByteSize(ExecutionContextScope *exe_scope) const
Return the size of the type in bytes.
bool IsIntegerType(bool &is_signed) const
bool IsVectorType(CompilerType *element_type=nullptr, uint64_t *size=nullptr) const
A uniqued constant string class.
Definition: ConstString.h:40
const char * GetCString() const
Get the string value as a C string.
Definition: ConstString.h:221
"lldb/Target/ExecutionContextScope.h" Inherit from this if your object can reconstruct its execution ...
llvm::StringRef GetString() const
A stream class that can stream formatted output to a file.
Definition: Stream.h:28
size_t Printf(const char *format,...) __attribute__((format(printf
Output printf formatted output to the stream.
Definition: Stream.cpp:107
size_t PutChar(char ch)
Definition: Stream.cpp:104
CompilerType GetCompilerType()
Definition: ValueObject.h:352
lldb::ValueObjectSP GetSP()
Definition: ValueObject.h:554
lldb::Format GetFormat() const
virtual lldb::ValueObjectSP GetSyntheticChildAtOffset(uint32_t offset, const CompilerType &type, bool can_create, ConstString name_const_str=ConstString())
lldb::ValueObjectSP GetChildAtIndex(size_t idx) override
Definition: VectorType.cpp:201
VectorTypeSyntheticFrontEnd(lldb::ValueObjectSP valobj_sp)
Definition: VectorType.cpp:194
size_t GetIndexOfChildWithName(ConstString name) override
Definition: VectorType.cpp:235
#define UINT32_MAX
Definition: lldb-defines.h:19
bool VectorTypeSummaryProvider(ValueObject &, Stream &, const TypeSummaryOptions &)
Definition: VectorType.cpp:253
SyntheticChildrenFrontEnd * VectorTypeSyntheticFrontEndCreator(CXXSyntheticChildren *, lldb::ValueObjectSP)
Definition: VectorType.cpp:291
size_t ExtractIndexFromString(const char *item_name)
A class that represents a running process on the host machine.
Definition: SBAttachInfo.h:14
Definition: SBAddress.h:15
@ eBasicTypeFloatComplex
@ eBasicTypeUnsignedInt
Format
Display format definitions.
@ eFormatCString
NULL terminated C strings.
@ eFormatCharArray
Print characters with no single quotes, used for character arrays that can contain non printable char...
@ eFormatInstruction
Disassemble an opcode.
@ eFormatVectorOfChar
@ eFormatVectorOfUInt64
@ eFormatVoid
Do not print this.
@ eFormatVectorOfSInt64
@ eFormatComplex
Floating point complex type.
@ eFormatHexFloat
ISO C99 hex float string.
@ eFormatBytesWithASCII
@ eFormatOSType
OS character codes encoded into an integer 'PICT' 'text' etc...
@ eFormatUnicode16
@ eFormatAddressInfo
Describe what an address points to (func + offset.
@ eFormatVectorOfUInt128
@ eFormatVectorOfUInt8
@ eFormatVectorOfFloat32
@ eFormatVectorOfSInt32
@ eFormatUnicode32
@ eFormatVectorOfSInt8
@ eFormatVectorOfUInt16
@ eFormatHexUppercase
@ eFormatVectorOfFloat64
@ eFormatCharPrintable
Only printable characters, '.' if not printable.
@ eFormatComplexInteger
Integer complex type.
@ eFormatVectorOfSInt16
@ eFormatVectorOfUInt32
@ eEncodingIEEE754
float
@ eEncodingUint
unsigned integer
@ eEncodingSint
signed integer
@ eDynamicDontRunTarget