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 
11 #include "lldb/Core/ValueObject.h"
14 #include "lldb/Symbol/TypeSystem.h"
15 #include "lldb/Target/Target.h"
16 
18 #include "lldb/Utility/Log.h"
19 
20 using namespace lldb;
21 using namespace lldb_private;
22 using namespace lldb_private::formatters;
23 
25  CompilerType element_type,
26  TypeSystem *type_system) {
27  lldbassert(type_system && "type_system needs to be not NULL");
28 
29  switch (format) {
32  return type_system->GetBuiltinTypeForEncodingAndBitSize(
33  eEncodingUint, 8 * type_system->GetPointerByteSize());
34 
36  return type_system->GetBasicTypeFromAST(lldb::eBasicTypeBool);
37 
38  case lldb::eFormatBytes:
40  case lldb::eFormatChar:
43  return type_system->GetBasicTypeFromAST(lldb::eBasicTypeChar);
44 
45  case lldb::eFormatComplex /* lldb::eFormatComplexFloat */:
47 
49  return type_system->GetBasicTypeFromAST(lldb::eBasicTypeChar)
50  .GetPointerType();
51 
52  case lldb::eFormatFloat:
53  return type_system->GetBasicTypeFromAST(lldb::eBasicTypeFloat);
54 
55  case lldb::eFormatHex:
57  case lldb::eFormatOctal:
58  return type_system->GetBasicTypeFromAST(lldb::eBasicTypeInt);
59 
61  return type_system->GetBasicTypeFromAST(lldb::eBasicTypeFloat);
62 
65 
68 
70  return type_system->GetBasicTypeFromAST(lldb::eBasicTypeChar);
71 
74  32);
75 
78  64);
79 
81  return type_system->GetBuiltinTypeForEncodingAndBitSize(eEncodingSint, 16);
82 
84  return type_system->GetBuiltinTypeForEncodingAndBitSize(eEncodingSint, 32);
85 
87  return type_system->GetBuiltinTypeForEncodingAndBitSize(eEncodingSint, 64);
88 
90  return type_system->GetBuiltinTypeForEncodingAndBitSize(eEncodingSint, 8);
91 
93  return type_system->GetBuiltinTypeForEncodingAndBitSize(eEncodingUint, 128);
94 
96  return type_system->GetBuiltinTypeForEncodingAndBitSize(eEncodingUint, 16);
97 
99  return type_system->GetBuiltinTypeForEncodingAndBitSize(eEncodingUint, 32);
100 
102  return type_system->GetBuiltinTypeForEncodingAndBitSize(eEncodingUint, 64);
103 
105  return type_system->GetBuiltinTypeForEncodingAndBitSize(eEncodingUint, 8);
106 
108  return element_type;
109 
110  case lldb::eFormatBinary:
113  case lldb::eFormatEnum:
115  case lldb::eFormatOSType:
116  case lldb::eFormatVoid:
117  default:
118  return type_system->GetBuiltinTypeForEncodingAndBitSize(eEncodingUint, 8);
119  }
120 }
121 
123  CompilerType element_type) {
124  switch (format) {
126  return lldb::eFormatChar;
127 
130  return lldb::eFormatFloat;
131 
136  return lldb::eFormatDecimal;
137 
143  return lldb::eFormatUnsigned;
144 
145  case lldb::eFormatBinary:
148  case lldb::eFormatEnum:
150  case lldb::eFormatOSType:
151  case lldb::eFormatVoid:
152  return eFormatHex;
153 
154  case lldb::eFormatDefault: {
155  // special case the (default, char) combination to actually display as an
156  // integer value most often, you won't want to see the ASCII characters...
157  // (and if you do, eFormatChar is a keystroke away)
158  bool is_char = element_type.IsCharType();
159  bool is_signed = false;
160  element_type.IsIntegerType(is_signed);
161  return is_char ? (is_signed ? lldb::eFormatDecimal : eFormatHex) : format;
162  } break;
163 
164  default:
165  return format;
166  }
167 }
168 
169 static size_t CalculateNumChildren(
170  CompilerType container_type, CompilerType element_type,
172  nullptr // does not matter here because all we trade in are basic types
173  ) {
174  llvm::Optional<uint64_t> container_size =
175  container_type.GetByteSize(exe_scope);
176  llvm::Optional<uint64_t> element_size = element_type.GetByteSize(exe_scope);
177 
178  if (container_size && element_size && *element_size) {
179  if (*container_size % *element_size)
180  return 0;
181  return *container_size / *element_size;
182  }
183  return 0;
184 }
185 
186 namespace lldb_private {
187 namespace formatters {
188 
190 public:
191  VectorTypeSyntheticFrontEnd(lldb::ValueObjectSP valobj_sp)
192  : SyntheticChildrenFrontEnd(*valobj_sp), m_child_type() {}
193 
194  ~VectorTypeSyntheticFrontEnd() override = default;
195 
196  size_t CalculateNumChildren() override { return m_num_children; }
197 
198  lldb::ValueObjectSP GetChildAtIndex(size_t idx) override {
199  if (idx >= CalculateNumChildren())
200  return {};
201  llvm::Optional<uint64_t> size = m_child_type.GetByteSize(nullptr);
202  if (!size)
203  return {};
204  auto offset = idx * *size;
205  StreamString idx_name;
206  idx_name.Printf("[%" PRIu64 "]", (uint64_t)idx);
207  ValueObjectSP child_sp(m_backend.GetSyntheticChildAtOffset(
208  offset, m_child_type, true, ConstString(idx_name.GetString())));
209  if (!child_sp)
210  return child_sp;
211 
212  child_sp->SetFormat(m_item_format);
213 
214  return child_sp;
215  }
216 
217  bool Update() override {
218  m_parent_format = m_backend.GetFormat();
219  CompilerType parent_type(m_backend.GetCompilerType());
220  CompilerType element_type;
221  parent_type.IsVectorType(&element_type);
222  m_child_type = ::GetCompilerTypeForFormat(m_parent_format, element_type,
223  parent_type.GetTypeSystem());
224  m_num_children = ::CalculateNumChildren(parent_type, m_child_type);
225  m_item_format = GetItemFormatForFormat(m_parent_format, m_child_type);
226  return false;
227  }
228 
229  bool MightHaveChildren() override { return true; }
230 
231  size_t GetIndexOfChildWithName(ConstString name) override {
232  const char *item_name = name.GetCString();
233  uint32_t idx = ExtractIndexFromString(item_name);
234  if (idx < UINT32_MAX && idx >= CalculateNumChildren())
235  return UINT32_MAX;
236  return idx;
237  }
238 
239 private:
240  lldb::Format m_parent_format = eFormatInvalid;
241  lldb::Format m_item_format = eFormatInvalid;
243  size_t m_num_children = 0;
244 };
245 
246 } // namespace formatters
247 } // namespace lldb_private
248 
250  ValueObject &valobj, Stream &s, const TypeSummaryOptions &) {
251  auto synthetic_children =
252  VectorTypeSyntheticFrontEndCreator(nullptr, valobj.GetSP());
253  if (!synthetic_children)
254  return false;
255 
256  synthetic_children->Update();
257 
258  s.PutChar('(');
259  bool first = true;
260 
261  size_t idx = 0, len = synthetic_children->CalculateNumChildren();
262 
263  for (; idx < len; idx++) {
264  auto child_sp = synthetic_children->GetChildAtIndex(idx);
265  if (!child_sp)
266  continue;
267  child_sp = child_sp->GetQualifiedRepresentationIfAvailable(
269 
270  const char *child_value = child_sp->GetValueAsCString();
271  if (child_value && *child_value) {
272  if (first) {
273  s.Printf("%s", child_value);
274  first = false;
275  } else {
276  s.Printf(", %s", child_value);
277  }
278  }
279  }
280 
281  s.PutChar(')');
282 
283  return true;
284 }
285 
288  CXXSyntheticChildren *, lldb::ValueObjectSP valobj_sp) {
289  if (!valobj_sp)
290  return nullptr;
291  return new VectorTypeSyntheticFrontEnd(valobj_sp);
292 }
lldb_private::formatters::VectorTypeSyntheticFrontEnd
Definition: VectorType.cpp:189
lldb::eFormatVectorOfUInt64
@ eFormatVectorOfUInt64
Definition: lldb-enumerations.h:187
lldb::eFormatOSType
@ eFormatOSType
OS character codes encoded into an integer 'PICT' 'text' etc...
Definition: lldb-enumerations.h:173
lldb::eFormatPointer
@ eFormatPointer
Definition: lldb-enumerations.h:178
lldb_private::CompilerType::IsCharType
bool IsCharType() const
Definition: CompilerType.cpp:77
lldb::eFormatVectorOfChar
@ eFormatVectorOfChar
Definition: lldb-enumerations.h:179
lldb_private::formatters::VectorTypeSyntheticFrontEnd::CalculateNumChildren
size_t CalculateNumChildren() override
Definition: VectorType.cpp:196
lldb::eFormatVectorOfUInt128
@ eFormatVectorOfUInt128
Definition: lldb-enumerations.h:191
VectorType.h
lldb::eBasicTypeBool
@ eBasicTypeBool
Definition: lldb-enumerations.h:769
lldb::Format
Format
Display format definitions.
Definition: lldb-enumerations.h:155
lldb_private::CompilerType::IsVectorType
bool IsVectorType(CompilerType *element_type=nullptr, uint64_t *size=nullptr) const
Definition: CompilerType.cpp:64
lldb_private::formatters::VectorTypeSyntheticFrontEnd::VectorTypeSyntheticFrontEnd
VectorTypeSyntheticFrontEnd(lldb::ValueObjectSP valobj_sp)
Definition: VectorType.cpp:191
lldb_private::formatters::VectorTypeSyntheticFrontEnd::MightHaveChildren
bool MightHaveChildren() override
Definition: VectorType.cpp:229
lldb::eFormatInstruction
@ eFormatInstruction
Disassemble an opcode.
Definition: lldb-enumerations.h:199
CalculateNumChildren
static size_t CalculateNumChildren(CompilerType container_type, CompilerType element_type, lldb_private::ExecutionContextScope *exe_scope=nullptr)
Definition: VectorType.cpp:169
lldb::eEncodingIEEE754
@ eEncodingIEEE754
float
Definition: lldb-enumerations.h:150
lldb::eFormatVectorOfSInt16
@ eFormatVectorOfSInt16
Definition: lldb-enumerations.h:182
lldb::eFormatVectorOfSInt32
@ eFormatVectorOfSInt32
Definition: lldb-enumerations.h:184
lldb_private::SyntheticChildrenFrontEnd
Definition: TypeSynthetic.h:27
lldb::eBasicTypeFloatComplex
@ eBasicTypeFloatComplex
Definition: lldb-enumerations.h:774
lldb::eFormatBoolean
@ eFormatBoolean
Definition: lldb-enumerations.h:158
lldb_private::CompilerType::GetByteSize
llvm::Optional< uint64_t > GetByteSize(ExecutionContextScope *exe_scope) const
Return the size of the type in bytes.
Definition: CompilerType.cpp:489
lldb::eBasicTypeChar
@ eBasicTypeChar
Definition: lldb-enumerations.h:750
lldb_private::Stream
Definition: Stream.h:28
lldb::eFormatBytesWithASCII
@ eFormatBytesWithASCII
Definition: lldb-enumerations.h:161
lldb_private::formatters::VectorTypeSummaryProvider
bool VectorTypeSummaryProvider(ValueObject &, Stream &, const TypeSummaryOptions &)
Definition: VectorType.cpp:249
lldb_private::StreamString::GetString
llvm::StringRef GetString() const
Definition: StreamString.cpp:51
lldb::eFormatVectorOfSInt8
@ eFormatVectorOfSInt8
Definition: lldb-enumerations.h:180
lldb_private::formatters::VectorTypeSyntheticFrontEnd::Update
bool Update() override
Definition: VectorType.cpp:217
Target.h
lldb::eBasicTypeFloat
@ eBasicTypeFloat
Definition: lldb-enumerations.h:771
lldb_private::TypeSystem::GetBuiltinTypeForEncodingAndBitSize
virtual CompilerType GetBuiltinTypeForEncodingAndBitSize(lldb::Encoding encoding, size_t bit_size)=0
lldb::eFormatBytes
@ eFormatBytes
Definition: lldb-enumerations.h:160
lldb::eFormatVectorOfFloat64
@ eFormatVectorOfFloat64
Definition: lldb-enumerations.h:190
lldb::eEncodingSint
@ eEncodingSint
signed integer
Definition: lldb-enumerations.h:149
lldb::eFormatVectorOfUInt32
@ eFormatVectorOfUInt32
Definition: lldb-enumerations.h:185
lldb::eDynamicDontRunTarget
@ eDynamicDontRunTarget
Definition: lldb-enumerations.h:497
LLDBAssert.h
Log.h
lldb_private::ExecutionContextScope
Definition: ExecutionContextScope.h:32
lldb_private::formatters::VectorTypeSyntheticFrontEndCreator
SyntheticChildrenFrontEnd * VectorTypeSyntheticFrontEndCreator(CXXSyntheticChildren *, lldb::ValueObjectSP)
Definition: VectorType.cpp:287
lldb_private::ValueObject::GetSP
lldb::ValueObjectSP GetSP()
Definition: ValueObject.h:555
lldb::eBasicTypeInt
@ eBasicTypeInt
Definition: lldb-enumerations.h:761
lldb::eEncodingUint
@ eEncodingUint
unsigned integer
Definition: lldb-enumerations.h:148
lldb_private::ConstString
Definition: ConstString.h:40
lldb_private::StreamString
Definition: StreamString.h:23
lldb_private::formatters::VectorTypeSyntheticFrontEnd::GetChildAtIndex
lldb::ValueObjectSP GetChildAtIndex(size_t idx) override
Definition: VectorType.cpp:198
lldb::eFormatDecimal
@ eFormatDecimal
Definition: lldb-enumerations.h:167
lldb::eFormatVectorOfUInt16
@ eFormatVectorOfUInt16
Definition: lldb-enumerations.h:183
lldb_private::CompilerType::IsIntegerType
bool IsIntegerType(bool &is_signed) const
Definition: CompilerType.cpp:141
TypeSystem.h
lldb_private::Stream::PutChar
size_t PutChar(char ch)
Definition: Stream.cpp:104
CompilerType.h
ValueObject.h
lldb::eFormatChar
@ eFormatChar
Definition: lldb-enumerations.h:162
lldb::eFormatHexFloat
@ eFormatHexFloat
ISO C99 hex float string.
Definition: lldb-enumerations.h:198
lldb::eBasicTypeUnsignedInt
@ eBasicTypeUnsignedInt
Definition: lldb-enumerations.h:762
lldb_private::formatters::VectorTypeSyntheticFrontEnd::m_child_type
CompilerType m_child_type
Definition: VectorType.cpp:242
lldb::eFormatVoid
@ eFormatVoid
Do not print this.
Definition: lldb-enumerations.h:200
lldb::eFormatUnicode16
@ eFormatUnicode16
Definition: lldb-enumerations.h:175
lldb::eFormatUnsigned
@ eFormatUnsigned
Definition: lldb-enumerations.h:177
GetCompilerTypeForFormat
static CompilerType GetCompilerTypeForFormat(lldb::Format format, CompilerType element_type, TypeSystem *type_system)
Definition: VectorType.cpp:24
lldbassert
#define lldbassert(x)
Definition: LLDBAssert.h:15
lldb_private::ValueObject
ValueObject:
Definition: ValueObject.h:105
lldb_private::CompilerType::GetPointerType
CompilerType GetPointerType() const
Return a new CompilerType that is a pointer to this type.
Definition: CompilerType.cpp:407
uint32_t
GetItemFormatForFormat
static lldb::Format GetItemFormatForFormat(lldb::Format format, CompilerType element_type)
Definition: VectorType.cpp:122
lldb::eFormatCString
@ eFormatCString
NULL terminated C strings.
Definition: lldb-enumerations.h:166
lldb_private::CompilerType::GetTypeSystem
TypeSystem * GetTypeSystem() const
Accessors.
Definition: CompilerType.h:162
lldb_private::formatters::VectorTypeSyntheticFrontEnd::GetIndexOfChildWithName
size_t GetIndexOfChildWithName(ConstString name) override
Definition: VectorType.cpp:231
lldb::eFormatVectorOfSInt64
@ eFormatVectorOfSInt64
Definition: lldb-enumerations.h:186
UINT32_MAX
#define UINT32_MAX
Definition: lldb-defines.h:19
lldb::eFormatCharPrintable
@ eFormatCharPrintable
Only printable characters, '.' if not printable.
Definition: lldb-enumerations.h:163
lldb_private::TypeSummaryOptions
Definition: TypeSummary.h:26
lldb::eFormatCharArray
@ eFormatCharArray
Print characters with no single quotes, used for character arrays that can contain non printable char...
Definition: lldb-enumerations.h:193
lldb_private::ConstString::GetCString
const char * GetCString() const
Get the string value as a C string.
Definition: ConstString.h:216
lldb_private::CompilerType
Generic representation of a type in a programming language.
Definition: CompilerType.h:33
lldb_private::Stream::Printf
size_t Printf(const char *format,...) __attribute__((format(printf
Output printf formatted output to the stream.
Definition: Stream.cpp:107
lldb::eFormatBinary
@ eFormatBinary
Definition: lldb-enumerations.h:159
lldb::eFormatComplexInteger
@ eFormatComplexInteger
Integer complex type.
Definition: lldb-enumerations.h:192
lldb_private::formatters
Definition: CXXFunctionPointer.h:15
lldb_private
A class that represents a running process on the host machine.
Definition: SBCommandInterpreterRunOptions.h:16
lldb::eFormatHex
@ eFormatHex
Definition: lldb-enumerations.h:169
lldb_private::TypeSystem
Interface for representing a type system.
Definition: TypeSystem.h:71
lldb::eFormatUnicode32
@ eFormatUnicode32
Definition: lldb-enumerations.h:176
lldb::eFormatEnum
@ eFormatEnum
Definition: lldb-enumerations.h:168
lldb_private::TypeSystem::GetPointerByteSize
virtual uint32_t GetPointerByteSize()=0
lldb::eFormatVectorOfFloat32
@ eFormatVectorOfFloat32
Definition: lldb-enumerations.h:189
lldb_private::CXXSyntheticChildren
Definition: TypeSynthetic.h:358
lldb_private::TypeSystem::GetBasicTypeFromAST
virtual CompilerType GetBasicTypeFromAST(lldb::BasicType basic_type)=0
lldb::eFormatFloat
@ eFormatFloat
Definition: lldb-enumerations.h:171
FormattersHelpers.h
lldb
Definition: SBAddress.h:15
lldb::eFormatAddressInfo
@ eFormatAddressInfo
Describe what an address points to (func + offset.
Definition: lldb-enumerations.h:196
lldb_private::formatters::ExtractIndexFromString
size_t ExtractIndexFromString(const char *item_name)
Definition: FormattersHelpers.cpp:119
lldb::eFormatVectorOfUInt8
@ eFormatVectorOfUInt8
Definition: lldb-enumerations.h:181
lldb::eFormatDefault
@ eFormatDefault
Definition: lldb-enumerations.h:156
lldb::eFormatComplex
@ eFormatComplex
Floating point complex type.
Definition: lldb-enumerations.h:164
lldb::eFormatOctal
@ eFormatOctal
Definition: lldb-enumerations.h:172
lldb::eFormatHexUppercase
@ eFormatHexUppercase
Definition: lldb-enumerations.h:170
lldb::eFormatInvalid
@ eFormatInvalid
Definition: lldb-enumerations.h:157