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 //===----------------------------------------------------------------------===//
10 #include "lldb/DataFormatters/VectorType.h"
12 #include "lldb/Core/ValueObject.h"
13 #include "lldb/DataFormatters/CXXFormatterFunctions.h"
14 #include "lldb/Symbol/ClangASTContext.h"
15 #include "lldb/Symbol/ClangASTType.h"
17 #include "lldb/Utility/LLDBAssert.h"
20 using namespace lldb_private;
21 using namespace lldb_private::formatters;
24 GetClangTypeForFormat (lldb::Format format,
25 ClangASTType element_type,
26 ClangASTContext *ast_ctx)
28 lldbassert(ast_ctx && "ast_ctx needs to be not NULL");
32 case lldb::eFormatAddressInfo:
33 case lldb::eFormatPointer:
34 return ast_ctx->GetPointerSizedIntType(false);
36 case lldb::eFormatBoolean:
37 return ast_ctx->GetBasicType(lldb::eBasicTypeBool);
39 case lldb::eFormatBytes:
40 case lldb::eFormatBytesWithASCII:
41 case lldb::eFormatChar:
42 case lldb::eFormatCharArray:
43 case lldb::eFormatCharPrintable:
44 return ast_ctx->GetBasicType(lldb::eBasicTypeChar);
46 case lldb::eFormatComplex /* lldb::eFormatComplexFloat */:
47 return ast_ctx->GetBasicType(lldb::eBasicTypeFloatComplex);
49 case lldb::eFormatCString:
50 return ast_ctx->GetBasicType(lldb::eBasicTypeChar).GetPointerType();
52 case lldb::eFormatFloat:
53 return ast_ctx->GetBasicType(lldb::eBasicTypeFloat);
55 case lldb::eFormatHex:
56 case lldb::eFormatHexUppercase:
57 case lldb::eFormatOctal:
58 return ast_ctx->GetBasicType(lldb::eBasicTypeInt);
60 case lldb::eFormatHexFloat:
61 return ast_ctx->GetBasicType(lldb::eBasicTypeFloat);
63 case lldb::eFormatUnicode16:
64 case lldb::eFormatUnicode32:
66 case lldb::eFormatUnsigned:
67 return ast_ctx->GetBasicType(lldb::eBasicTypeUnsignedInt);
69 case lldb::eFormatVectorOfChar:
70 return ast_ctx->GetBasicType(lldb::eBasicTypeChar);
72 case lldb::eFormatVectorOfFloat32:
73 return ast_ctx->GetFloatTypeFromBitSize(32);
75 case lldb::eFormatVectorOfFloat64:
76 return ast_ctx->GetFloatTypeFromBitSize(64);
78 case lldb::eFormatVectorOfSInt16:
79 return ast_ctx->GetIntTypeFromBitSize(16, true);
81 case lldb::eFormatVectorOfSInt32:
82 return ast_ctx->GetIntTypeFromBitSize(32, true);
84 case lldb::eFormatVectorOfSInt64:
85 return ast_ctx->GetIntTypeFromBitSize(64, true);
87 case lldb::eFormatVectorOfSInt8:
88 return ast_ctx->GetIntTypeFromBitSize(8, true);
90 case lldb::eFormatVectorOfUInt128:
91 return ast_ctx->GetIntTypeFromBitSize(128, false);
93 case lldb::eFormatVectorOfUInt16:
94 return ast_ctx->GetIntTypeFromBitSize(16, false);
96 case lldb::eFormatVectorOfUInt32:
97 return ast_ctx->GetIntTypeFromBitSize(32, false);
99 case lldb::eFormatVectorOfUInt64:
100 return ast_ctx->GetIntTypeFromBitSize(64, false);
102 case lldb::eFormatVectorOfUInt8:
103 return ast_ctx->GetIntTypeFromBitSize(8, false);
105 case lldb::eFormatDefault:
108 case lldb::eFormatBinary:
109 case lldb::eFormatComplexInteger:
110 case lldb::eFormatDecimal:
111 case lldb::eFormatEnum:
112 case lldb::eFormatInstruction:
113 case lldb::eFormatOSType:
114 case lldb::eFormatVoid:
116 return ast_ctx->GetIntTypeFromBitSize(8, false);
121 GetItemFormatForFormat (lldb::Format format,
122 ClangASTType element_type)
126 case lldb::eFormatVectorOfChar:
127 return lldb::eFormatChar;
129 case lldb::eFormatVectorOfFloat32:
130 case lldb::eFormatVectorOfFloat64:
131 return lldb::eFormatFloat;
133 case lldb::eFormatVectorOfSInt16:
134 case lldb::eFormatVectorOfSInt32:
135 case lldb::eFormatVectorOfSInt64:
136 case lldb::eFormatVectorOfSInt8:
137 return lldb::eFormatDecimal;
139 case lldb::eFormatVectorOfUInt128:
140 case lldb::eFormatVectorOfUInt16:
141 case lldb::eFormatVectorOfUInt32:
142 case lldb::eFormatVectorOfUInt64:
143 case lldb::eFormatVectorOfUInt8:
144 return lldb::eFormatUnsigned;
146 case lldb::eFormatBinary:
147 case lldb::eFormatComplexInteger:
148 case lldb::eFormatDecimal:
149 case lldb::eFormatEnum:
150 case lldb::eFormatInstruction:
151 case lldb::eFormatOSType:
152 case lldb::eFormatVoid:
155 case lldb::eFormatDefault:
157 // special case the (default, char) combination to actually display as an integer value
158 // most often, you won't want to see the ASCII characters... (and if you do, eFormatChar is a keystroke away)
159 bool is_char = element_type.IsCharType();
160 bool is_signed = false;
161 element_type.IsIntegerType(is_signed);
162 return is_char ? (is_signed ? lldb::eFormatDecimal : eFormatHex) : format;
172 CalculateNumChildren (ClangASTType container_type,
173 ClangASTType element_type,
174 lldb_private::ExecutionContextScope *exe_scope = nullptr // does not matter here because all we trade in are basic types
177 auto container_size = container_type.GetByteSize(exe_scope);
178 auto element_size = element_type.GetByteSize(exe_scope);
182 if (container_size % element_size)
184 return container_size / element_size;
189 namespace lldb_private {
190 namespace formatters {
192 class VectorTypeSyntheticFrontEnd : public SyntheticChildrenFrontEnd
195 VectorTypeSyntheticFrontEnd (lldb::ValueObjectSP valobj_sp) :
196 SyntheticChildrenFrontEnd(*valobj_sp),
197 m_parent_format (eFormatInvalid),
198 m_item_format(eFormatInvalid),
204 CalculateNumChildren ()
206 return m_num_children;
209 virtual lldb::ValueObjectSP
210 GetChildAtIndex (size_t idx)
212 if (idx >= CalculateNumChildren())
213 return lldb::ValueObjectSP();
214 auto offset = idx * m_child_type.GetByteSize(nullptr);
215 ValueObjectSP child_sp(m_backend.GetSyntheticChildAtOffset(offset, m_child_type, true));
219 StreamString idx_name;
220 idx_name.Printf("[%" PRIu64 "]", (uint64_t)idx);
221 child_sp->SetName( ConstString( idx_name.GetData() ) );
223 child_sp->SetFormat(m_item_format);
231 m_parent_format = m_backend.GetFormat();
232 ClangASTType parent_type(m_backend.GetClangType());
233 ClangASTType element_type;
234 parent_type.IsVectorType(&element_type, nullptr);
235 m_child_type = ::GetClangTypeForFormat(m_parent_format, element_type, ClangASTContext::GetASTContext(parent_type.GetASTContext()));
236 m_num_children = ::CalculateNumChildren(parent_type,
238 m_item_format = GetItemFormatForFormat(m_parent_format,
250 GetIndexOfChildWithName (const ConstString &name)
252 const char* item_name = name.GetCString();
253 uint32_t idx = ExtractIndexFromString(item_name);
254 if (idx < UINT32_MAX && idx >= CalculateNumChildren())
260 ~VectorTypeSyntheticFrontEnd () {}
263 lldb::Format m_parent_format;
264 lldb::Format m_item_format;
265 ClangASTType m_child_type;
266 size_t m_num_children;
272 lldb_private::formatters::VectorTypeSummaryProvider (ValueObject& valobj,
274 const TypeSummaryOptions&)
276 auto synthetic_children = VectorTypeSyntheticFrontEndCreator(nullptr, valobj.GetSP());
277 if (!synthetic_children)
280 synthetic_children->Update();
285 size_t idx = 0, len = synthetic_children->CalculateNumChildren();
291 auto child_sp = synthetic_children->GetChildAtIndex(idx);
294 child_sp = child_sp->GetQualifiedRepresentationIfAvailable(lldb::eDynamicDontRunTarget, true);
296 const char* child_value = child_sp->GetValueAsCString();
297 if (child_value && *child_value)
301 s.Printf("%s", child_value);
306 s.Printf(", %s", child_value);
316 lldb_private::SyntheticChildrenFrontEnd*
317 lldb_private::formatters::VectorTypeSyntheticFrontEndCreator (CXXSyntheticChildren*, lldb::ValueObjectSP valobj_sp)
321 return (new VectorTypeSyntheticFrontEnd(valobj_sp));