1 //===-- VectorType.cpp ------------------------------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
12 // Other libraries and framework includes
14 #include "lldb/DataFormatters/VectorType.h"
16 #include "lldb/Core/ValueObject.h"
17 #include "lldb/DataFormatters/FormattersHelpers.h"
18 #include "lldb/Symbol/CompilerType.h"
19 #include "lldb/Symbol/TypeSystem.h"
20 #include "lldb/Target/Target.h"
22 #include "lldb/Utility/LLDBAssert.h"
25 using namespace lldb_private;
26 using namespace lldb_private::formatters;
28 static CompilerType GetCompilerTypeForFormat(lldb::Format format,
29 CompilerType element_type,
30 TypeSystem *type_system) {
31 lldbassert(type_system && "type_system needs to be not NULL");
34 case lldb::eFormatAddressInfo:
35 case lldb::eFormatPointer:
36 return type_system->GetBuiltinTypeForEncodingAndBitSize(
37 eEncodingUint, 8 * type_system->GetPointerByteSize());
39 case lldb::eFormatBoolean:
40 return type_system->GetBasicTypeFromAST(lldb::eBasicTypeBool);
42 case lldb::eFormatBytes:
43 case lldb::eFormatBytesWithASCII:
44 case lldb::eFormatChar:
45 case lldb::eFormatCharArray:
46 case lldb::eFormatCharPrintable:
47 return type_system->GetBasicTypeFromAST(lldb::eBasicTypeChar);
49 case lldb::eFormatComplex /* lldb::eFormatComplexFloat */:
50 return type_system->GetBasicTypeFromAST(lldb::eBasicTypeFloatComplex);
52 case lldb::eFormatCString:
53 return type_system->GetBasicTypeFromAST(lldb::eBasicTypeChar)
56 case lldb::eFormatFloat:
57 return type_system->GetBasicTypeFromAST(lldb::eBasicTypeFloat);
59 case lldb::eFormatHex:
60 case lldb::eFormatHexUppercase:
61 case lldb::eFormatOctal:
62 return type_system->GetBasicTypeFromAST(lldb::eBasicTypeInt);
64 case lldb::eFormatHexFloat:
65 return type_system->GetBasicTypeFromAST(lldb::eBasicTypeFloat);
67 case lldb::eFormatUnicode16:
68 case lldb::eFormatUnicode32:
70 case lldb::eFormatUnsigned:
71 return type_system->GetBasicTypeFromAST(lldb::eBasicTypeUnsignedInt);
73 case lldb::eFormatVectorOfChar:
74 return type_system->GetBasicTypeFromAST(lldb::eBasicTypeChar);
76 case lldb::eFormatVectorOfFloat32:
77 return type_system->GetBuiltinTypeForEncodingAndBitSize(eEncodingIEEE754,
80 case lldb::eFormatVectorOfFloat64:
81 return type_system->GetBuiltinTypeForEncodingAndBitSize(eEncodingIEEE754,
84 case lldb::eFormatVectorOfSInt16:
85 return type_system->GetBuiltinTypeForEncodingAndBitSize(eEncodingSint, 16);
87 case lldb::eFormatVectorOfSInt32:
88 return type_system->GetBuiltinTypeForEncodingAndBitSize(eEncodingSint, 32);
90 case lldb::eFormatVectorOfSInt64:
91 return type_system->GetBuiltinTypeForEncodingAndBitSize(eEncodingSint, 64);
93 case lldb::eFormatVectorOfSInt8:
94 return type_system->GetBuiltinTypeForEncodingAndBitSize(eEncodingSint, 8);
96 case lldb::eFormatVectorOfUInt128:
97 return type_system->GetBuiltinTypeForEncodingAndBitSize(eEncodingUint, 128);
99 case lldb::eFormatVectorOfUInt16:
100 return type_system->GetBuiltinTypeForEncodingAndBitSize(eEncodingUint, 16);
102 case lldb::eFormatVectorOfUInt32:
103 return type_system->GetBuiltinTypeForEncodingAndBitSize(eEncodingUint, 32);
105 case lldb::eFormatVectorOfUInt64:
106 return type_system->GetBuiltinTypeForEncodingAndBitSize(eEncodingUint, 64);
108 case lldb::eFormatVectorOfUInt8:
109 return type_system->GetBuiltinTypeForEncodingAndBitSize(eEncodingUint, 8);
111 case lldb::eFormatDefault:
114 case lldb::eFormatBinary:
115 case lldb::eFormatComplexInteger:
116 case lldb::eFormatDecimal:
117 case lldb::eFormatEnum:
118 case lldb::eFormatInstruction:
119 case lldb::eFormatOSType:
120 case lldb::eFormatVoid:
122 return type_system->GetBuiltinTypeForEncodingAndBitSize(eEncodingUint, 8);
126 static lldb::Format GetItemFormatForFormat(lldb::Format format,
127 CompilerType element_type) {
129 case lldb::eFormatVectorOfChar:
130 return lldb::eFormatChar;
132 case lldb::eFormatVectorOfFloat32:
133 case lldb::eFormatVectorOfFloat64:
134 return lldb::eFormatFloat;
136 case lldb::eFormatVectorOfSInt16:
137 case lldb::eFormatVectorOfSInt32:
138 case lldb::eFormatVectorOfSInt64:
139 case lldb::eFormatVectorOfSInt8:
140 return lldb::eFormatDecimal;
142 case lldb::eFormatVectorOfUInt128:
143 case lldb::eFormatVectorOfUInt16:
144 case lldb::eFormatVectorOfUInt32:
145 case lldb::eFormatVectorOfUInt64:
146 case lldb::eFormatVectorOfUInt8:
147 return lldb::eFormatUnsigned;
149 case lldb::eFormatBinary:
150 case lldb::eFormatComplexInteger:
151 case lldb::eFormatDecimal:
152 case lldb::eFormatEnum:
153 case lldb::eFormatInstruction:
154 case lldb::eFormatOSType:
155 case lldb::eFormatVoid:
158 case lldb::eFormatDefault: {
159 // special case the (default, char) combination to actually display as an
161 // most often, you won't want to see the ASCII characters... (and if you do,
162 // eFormatChar is a keystroke away)
163 bool is_char = element_type.IsCharType();
164 bool is_signed = false;
165 element_type.IsIntegerType(is_signed);
166 return is_char ? (is_signed ? lldb::eFormatDecimal : eFormatHex) : format;
174 static size_t CalculateNumChildren(
175 CompilerType container_type, CompilerType element_type,
176 lldb_private::ExecutionContextScope *exe_scope =
177 nullptr // does not matter here because all we trade in are basic types
179 auto container_size = container_type.GetByteSize(exe_scope);
180 auto element_size = element_type.GetByteSize(exe_scope);
183 if (container_size % element_size)
185 return container_size / element_size;
190 namespace lldb_private {
191 namespace formatters {
193 class VectorTypeSyntheticFrontEnd : public SyntheticChildrenFrontEnd {
195 VectorTypeSyntheticFrontEnd(lldb::ValueObjectSP valobj_sp)
196 : SyntheticChildrenFrontEnd(*valobj_sp), m_parent_format(eFormatInvalid),
197 m_item_format(eFormatInvalid), m_child_type(), m_num_children(0) {}
199 ~VectorTypeSyntheticFrontEnd() override = default;
201 size_t CalculateNumChildren() override { return m_num_children; }
203 lldb::ValueObjectSP GetChildAtIndex(size_t idx) override {
204 if (idx >= CalculateNumChildren())
205 return lldb::ValueObjectSP();
206 auto offset = idx * m_child_type.GetByteSize(nullptr);
207 ValueObjectSP child_sp(
208 m_backend.GetSyntheticChildAtOffset(offset, m_child_type, true));
212 StreamString idx_name;
213 idx_name.Printf("[%" PRIu64 "]", (uint64_t)idx);
214 child_sp->SetName(ConstString(idx_name.GetString()));
216 child_sp->SetFormat(m_item_format);
221 bool Update() override {
222 m_parent_format = m_backend.GetFormat();
223 CompilerType parent_type(m_backend.GetCompilerType());
224 CompilerType element_type;
225 parent_type.IsVectorType(&element_type, nullptr);
226 TargetSP target_sp(m_backend.GetTargetSP());
227 m_child_type = ::GetCompilerTypeForFormat(
228 m_parent_format, element_type,
230 ? target_sp->GetScratchTypeSystemForLanguage(nullptr,
231 lldb::eLanguageTypeC)
233 m_num_children = ::CalculateNumChildren(parent_type, m_child_type);
234 m_item_format = GetItemFormatForFormat(m_parent_format, m_child_type);
238 bool MightHaveChildren() override { return true; }
240 size_t GetIndexOfChildWithName(const ConstString &name) override {
241 const char *item_name = name.GetCString();
242 uint32_t idx = ExtractIndexFromString(item_name);
243 if (idx < UINT32_MAX && idx >= CalculateNumChildren())
249 lldb::Format m_parent_format;
250 lldb::Format m_item_format;
251 CompilerType m_child_type;
252 size_t m_num_children;
255 } // namespace formatters
256 } // namespace lldb_private
258 bool lldb_private::formatters::VectorTypeSummaryProvider(
259 ValueObject &valobj, Stream &s, const TypeSummaryOptions &) {
260 auto synthetic_children =
261 VectorTypeSyntheticFrontEndCreator(nullptr, valobj.GetSP());
262 if (!synthetic_children)
265 synthetic_children->Update();
270 size_t idx = 0, len = synthetic_children->CalculateNumChildren();
272 for (; idx < len; idx++) {
273 auto child_sp = synthetic_children->GetChildAtIndex(idx);
276 child_sp = child_sp->GetQualifiedRepresentationIfAvailable(
277 lldb::eDynamicDontRunTarget, true);
279 const char *child_value = child_sp->GetValueAsCString();
280 if (child_value && *child_value) {
282 s.Printf("%s", child_value);
285 s.Printf(", %s", child_value);
295 lldb_private::SyntheticChildrenFrontEnd *
296 lldb_private::formatters::VectorTypeSyntheticFrontEndCreator(
297 CXXSyntheticChildren *, lldb::ValueObjectSP valobj_sp) {
300 return new VectorTypeSyntheticFrontEnd(valobj_sp);