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;
29 GetCompilerTypeForFormat (lldb::Format format,
30 CompilerType element_type,
31 TypeSystem *type_system)
33 lldbassert(type_system && "type_system needs to be not NULL");
37 case lldb::eFormatAddressInfo:
38 case lldb::eFormatPointer:
39 return type_system->GetBuiltinTypeForEncodingAndBitSize (eEncodingUint, 8*type_system->GetPointerByteSize());
41 case lldb::eFormatBoolean:
42 return type_system->GetBasicTypeFromAST(lldb::eBasicTypeBool);
44 case lldb::eFormatBytes:
45 case lldb::eFormatBytesWithASCII:
46 case lldb::eFormatChar:
47 case lldb::eFormatCharArray:
48 case lldb::eFormatCharPrintable:
49 return type_system->GetBasicTypeFromAST(lldb::eBasicTypeChar);
51 case lldb::eFormatComplex /* lldb::eFormatComplexFloat */:
52 return type_system->GetBasicTypeFromAST(lldb::eBasicTypeFloatComplex);
54 case lldb::eFormatCString:
55 return type_system->GetBasicTypeFromAST(lldb::eBasicTypeChar).GetPointerType();
57 case lldb::eFormatFloat:
58 return type_system->GetBasicTypeFromAST(lldb::eBasicTypeFloat);
60 case lldb::eFormatHex:
61 case lldb::eFormatHexUppercase:
62 case lldb::eFormatOctal:
63 return type_system->GetBasicTypeFromAST(lldb::eBasicTypeInt);
65 case lldb::eFormatHexFloat:
66 return type_system->GetBasicTypeFromAST(lldb::eBasicTypeFloat);
68 case lldb::eFormatUnicode16:
69 case lldb::eFormatUnicode32:
71 case lldb::eFormatUnsigned:
72 return type_system->GetBasicTypeFromAST(lldb::eBasicTypeUnsignedInt);
74 case lldb::eFormatVectorOfChar:
75 return type_system->GetBasicTypeFromAST(lldb::eBasicTypeChar);
77 case lldb::eFormatVectorOfFloat32:
78 return type_system->GetBuiltinTypeForEncodingAndBitSize(eEncodingIEEE754, 32);
80 case lldb::eFormatVectorOfFloat64:
81 return type_system->GetBuiltinTypeForEncodingAndBitSize(eEncodingIEEE754, 64);
83 case lldb::eFormatVectorOfSInt16:
84 return type_system->GetBuiltinTypeForEncodingAndBitSize (eEncodingSint, 16);
86 case lldb::eFormatVectorOfSInt32:
87 return type_system->GetBuiltinTypeForEncodingAndBitSize (eEncodingSint, 32);
89 case lldb::eFormatVectorOfSInt64:
90 return type_system->GetBuiltinTypeForEncodingAndBitSize (eEncodingSint, 64);
92 case lldb::eFormatVectorOfSInt8:
93 return type_system->GetBuiltinTypeForEncodingAndBitSize (eEncodingSint, 8);
95 case lldb::eFormatVectorOfUInt128:
96 return type_system->GetBuiltinTypeForEncodingAndBitSize (eEncodingUint, 128);
98 case lldb::eFormatVectorOfUInt16:
99 return type_system->GetBuiltinTypeForEncodingAndBitSize (eEncodingUint, 16);
101 case lldb::eFormatVectorOfUInt32:
102 return type_system->GetBuiltinTypeForEncodingAndBitSize (eEncodingUint, 32);
104 case lldb::eFormatVectorOfUInt64:
105 return type_system->GetBuiltinTypeForEncodingAndBitSize (eEncodingUint, 64);
107 case lldb::eFormatVectorOfUInt8:
108 return type_system->GetBuiltinTypeForEncodingAndBitSize (eEncodingUint, 8);
110 case lldb::eFormatDefault:
113 case lldb::eFormatBinary:
114 case lldb::eFormatComplexInteger:
115 case lldb::eFormatDecimal:
116 case lldb::eFormatEnum:
117 case lldb::eFormatInstruction:
118 case lldb::eFormatOSType:
119 case lldb::eFormatVoid:
121 return type_system->GetBuiltinTypeForEncodingAndBitSize (eEncodingUint, 8);
126 GetItemFormatForFormat (lldb::Format format,
127 CompilerType element_type)
131 case lldb::eFormatVectorOfChar:
132 return lldb::eFormatChar;
134 case lldb::eFormatVectorOfFloat32:
135 case lldb::eFormatVectorOfFloat64:
136 return lldb::eFormatFloat;
138 case lldb::eFormatVectorOfSInt16:
139 case lldb::eFormatVectorOfSInt32:
140 case lldb::eFormatVectorOfSInt64:
141 case lldb::eFormatVectorOfSInt8:
142 return lldb::eFormatDecimal;
144 case lldb::eFormatVectorOfUInt128:
145 case lldb::eFormatVectorOfUInt16:
146 case lldb::eFormatVectorOfUInt32:
147 case lldb::eFormatVectorOfUInt64:
148 case lldb::eFormatVectorOfUInt8:
149 return lldb::eFormatUnsigned;
151 case lldb::eFormatBinary:
152 case lldb::eFormatComplexInteger:
153 case lldb::eFormatDecimal:
154 case lldb::eFormatEnum:
155 case lldb::eFormatInstruction:
156 case lldb::eFormatOSType:
157 case lldb::eFormatVoid:
160 case lldb::eFormatDefault:
162 // special case the (default, char) combination to actually display as an integer value
163 // most often, you won't want to see the ASCII characters... (and if you do, eFormatChar is a keystroke away)
164 bool is_char = element_type.IsCharType();
165 bool is_signed = false;
166 element_type.IsIntegerType(is_signed);
167 return is_char ? (is_signed ? lldb::eFormatDecimal : eFormatHex) : format;
177 CalculateNumChildren (CompilerType container_type,
178 CompilerType element_type,
179 lldb_private::ExecutionContextScope *exe_scope = nullptr // does not matter here because all we trade in are basic types
182 auto container_size = container_type.GetByteSize(exe_scope);
183 auto element_size = element_type.GetByteSize(exe_scope);
187 if (container_size % element_size)
189 return container_size / element_size;
194 namespace lldb_private {
195 namespace formatters {
197 class VectorTypeSyntheticFrontEnd : public SyntheticChildrenFrontEnd
200 VectorTypeSyntheticFrontEnd (lldb::ValueObjectSP valobj_sp) :
201 SyntheticChildrenFrontEnd(*valobj_sp),
202 m_parent_format (eFormatInvalid),
203 m_item_format(eFormatInvalid),
208 ~VectorTypeSyntheticFrontEnd() override = default;
211 CalculateNumChildren() override
213 return m_num_children;
217 GetChildAtIndex(size_t idx) override
219 if (idx >= CalculateNumChildren())
220 return lldb::ValueObjectSP();
221 auto offset = idx * m_child_type.GetByteSize(nullptr);
222 ValueObjectSP child_sp(m_backend.GetSyntheticChildAtOffset(offset, m_child_type, true));
226 StreamString idx_name;
227 idx_name.Printf("[%" PRIu64 "]", (uint64_t)idx);
228 child_sp->SetName( ConstString( idx_name.GetData() ) );
230 child_sp->SetFormat(m_item_format);
238 m_parent_format = m_backend.GetFormat();
239 CompilerType parent_type(m_backend.GetCompilerType());
240 CompilerType element_type;
241 parent_type.IsVectorType(&element_type, nullptr);
242 TargetSP target_sp(m_backend.GetTargetSP());
243 m_child_type = ::GetCompilerTypeForFormat(m_parent_format,
245 target_sp ? target_sp->GetScratchTypeSystemForLanguage(nullptr, lldb::eLanguageTypeC) : nullptr);
246 m_num_children = ::CalculateNumChildren(parent_type,
248 m_item_format = GetItemFormatForFormat(m_parent_format,
254 MightHaveChildren() override
260 GetIndexOfChildWithName(const ConstString &name) override
262 const char* item_name = name.GetCString();
263 uint32_t idx = ExtractIndexFromString(item_name);
264 if (idx < UINT32_MAX && idx >= CalculateNumChildren())
270 lldb::Format m_parent_format;
271 lldb::Format m_item_format;
272 CompilerType m_child_type;
273 size_t m_num_children;
276 } // namespace formatters
277 } // namespace lldb_private
280 lldb_private::formatters::VectorTypeSummaryProvider (ValueObject& valobj,
282 const TypeSummaryOptions&)
284 auto synthetic_children = VectorTypeSyntheticFrontEndCreator(nullptr, valobj.GetSP());
285 if (!synthetic_children)
288 synthetic_children->Update();
293 size_t idx = 0, len = synthetic_children->CalculateNumChildren();
299 auto child_sp = synthetic_children->GetChildAtIndex(idx);
302 child_sp = child_sp->GetQualifiedRepresentationIfAvailable(lldb::eDynamicDontRunTarget, true);
304 const char* child_value = child_sp->GetValueAsCString();
305 if (child_value && *child_value)
309 s.Printf("%s", child_value);
314 s.Printf(", %s", child_value);
324 lldb_private::SyntheticChildrenFrontEnd*
325 lldb_private::formatters::VectorTypeSyntheticFrontEndCreator (CXXSyntheticChildren*, lldb::ValueObjectSP valobj_sp)
329 return new VectorTypeSyntheticFrontEnd(valobj_sp);