LLDB  mainline
VectorType.cpp
Go to the documentation of this file.
1 //===-- VectorType.cpp ------------------------------------------*- C++ -*-===//
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 
19 using namespace lldb;
20 using namespace lldb_private;
21 using namespace lldb_private::formatters;
22 
24  CompilerType element_type,
25  TypeSystem *type_system) {
26  lldbassert(type_system && "type_system needs to be not NULL");
27 
28  switch (format) {
31  return type_system->GetBuiltinTypeForEncodingAndBitSize(
32  eEncodingUint, 8 * type_system->GetPointerByteSize());
33 
35  return type_system->GetBasicTypeFromAST(lldb::eBasicTypeBool);
36 
37  case lldb::eFormatBytes:
39  case lldb::eFormatChar:
42  return type_system->GetBasicTypeFromAST(lldb::eBasicTypeChar);
43 
44  case lldb::eFormatComplex /* lldb::eFormatComplexFloat */:
46 
48  return type_system->GetBasicTypeFromAST(lldb::eBasicTypeChar)
49  .GetPointerType();
50 
51  case lldb::eFormatFloat:
52  return type_system->GetBasicTypeFromAST(lldb::eBasicTypeFloat);
53 
54  case lldb::eFormatHex:
56  case lldb::eFormatOctal:
57  return type_system->GetBasicTypeFromAST(lldb::eBasicTypeInt);
58 
60  return type_system->GetBasicTypeFromAST(lldb::eBasicTypeFloat);
61 
64 
67 
69  return type_system->GetBasicTypeFromAST(lldb::eBasicTypeChar);
70 
73  32);
74 
77  64);
78 
80  return type_system->GetBuiltinTypeForEncodingAndBitSize(eEncodingSint, 16);
81 
83  return type_system->GetBuiltinTypeForEncodingAndBitSize(eEncodingSint, 32);
84 
86  return type_system->GetBuiltinTypeForEncodingAndBitSize(eEncodingSint, 64);
87 
89  return type_system->GetBuiltinTypeForEncodingAndBitSize(eEncodingSint, 8);
90 
92  return type_system->GetBuiltinTypeForEncodingAndBitSize(eEncodingUint, 128);
93 
95  return type_system->GetBuiltinTypeForEncodingAndBitSize(eEncodingUint, 16);
96 
98  return type_system->GetBuiltinTypeForEncodingAndBitSize(eEncodingUint, 32);
99 
101  return type_system->GetBuiltinTypeForEncodingAndBitSize(eEncodingUint, 64);
102 
104  return type_system->GetBuiltinTypeForEncodingAndBitSize(eEncodingUint, 8);
105 
107  return element_type;
108 
109  case lldb::eFormatBinary:
112  case lldb::eFormatEnum:
114  case lldb::eFormatOSType:
115  case lldb::eFormatVoid:
116  default:
117  return type_system->GetBuiltinTypeForEncodingAndBitSize(eEncodingUint, 8);
118  }
119 }
120 
122  CompilerType element_type) {
123  switch (format) {
125  return lldb::eFormatChar;
126 
129  return lldb::eFormatFloat;
130 
135  return lldb::eFormatDecimal;
136 
142  return lldb::eFormatUnsigned;
143 
144  case lldb::eFormatBinary:
147  case lldb::eFormatEnum:
149  case lldb::eFormatOSType:
150  case lldb::eFormatVoid:
151  return eFormatHex;
152 
153  case lldb::eFormatDefault: {
154  // special case the (default, char) combination to actually display as an
155  // integer value most often, you won't want to see the ASCII characters...
156  // (and if you do, eFormatChar is a keystroke away)
157  bool is_char = element_type.IsCharType();
158  bool is_signed = false;
159  element_type.IsIntegerType(is_signed);
160  return is_char ? (is_signed ? lldb::eFormatDecimal : eFormatHex) : format;
161  } break;
162 
163  default:
164  return format;
165  }
166 }
167 
168 static size_t CalculateNumChildren(
169  CompilerType container_type, CompilerType element_type,
171  nullptr // does not matter here because all we trade in are basic types
172  ) {
173  llvm::Optional<uint64_t> container_size =
174  container_type.GetByteSize(exe_scope);
175  llvm::Optional<uint64_t> element_size = element_type.GetByteSize(exe_scope);
176 
177  if (container_size && element_size && *element_size) {
178  if (*container_size % *element_size)
179  return 0;
180  return *container_size / *element_size;
181  }
182  return 0;
183 }
184 
185 namespace lldb_private {
186 namespace formatters {
187 
189 public:
190  VectorTypeSyntheticFrontEnd(lldb::ValueObjectSP valobj_sp)
191  : SyntheticChildrenFrontEnd(*valobj_sp), m_parent_format(eFormatInvalid),
192  m_item_format(eFormatInvalid), m_child_type(), m_num_children(0) {}
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, nullptr);
222  TargetSP target_sp(m_backend.GetTargetSP());
223  m_child_type = ::GetCompilerTypeForFormat(
224  m_parent_format, element_type,
225  target_sp
226  ? target_sp->GetScratchTypeSystemForLanguage(nullptr,
228  : nullptr);
229  m_num_children = ::CalculateNumChildren(parent_type, m_child_type);
230  m_item_format = GetItemFormatForFormat(m_parent_format, m_child_type);
231  return false;
232  }
233 
234  bool MightHaveChildren() override { return true; }
235 
236  size_t GetIndexOfChildWithName(ConstString name) override {
237  const char *item_name = name.GetCString();
238  uint32_t idx = ExtractIndexFromString(item_name);
239  if (idx < UINT32_MAX && idx >= CalculateNumChildren())
240  return UINT32_MAX;
241  return idx;
242  }
243 
244 private:
245  lldb::Format m_parent_format;
246  lldb::Format m_item_format;
247  CompilerType m_child_type;
248  size_t m_num_children;
249 };
250 
251 } // namespace formatters
252 } // namespace lldb_private
253 
255  ValueObject &valobj, Stream &s, const TypeSummaryOptions &) {
256  auto synthetic_children =
257  VectorTypeSyntheticFrontEndCreator(nullptr, valobj.GetSP());
258  if (!synthetic_children)
259  return false;
260 
261  synthetic_children->Update();
262 
263  s.PutChar('(');
264  bool first = true;
265 
266  size_t idx = 0, len = synthetic_children->CalculateNumChildren();
267 
268  for (; idx < len; idx++) {
269  auto child_sp = synthetic_children->GetChildAtIndex(idx);
270  if (!child_sp)
271  continue;
272  child_sp = child_sp->GetQualifiedRepresentationIfAvailable(
274 
275  const char *child_value = child_sp->GetValueAsCString();
276  if (child_value && *child_value) {
277  if (first) {
278  s.Printf("%s", child_value);
279  first = false;
280  } else {
281  s.Printf(", %s", child_value);
282  }
283  }
284  }
285 
286  s.PutChar(')');
287 
288  return true;
289 }
290 
293  CXXSyntheticChildren *, lldb::ValueObjectSP valobj_sp) {
294  if (!valobj_sp)
295  return nullptr;
296  return new VectorTypeSyntheticFrontEnd(valobj_sp);
297 }
Enumerations for broadcasting.
Definition: SBLaunchInfo.h:14
A stream class that can stream formatted output to a file.
Definition: Stream.h:28
#define lldbassert(x)
Definition: LLDBAssert.h:15
static lldb::Format GetItemFormatForFormat(lldb::Format format, CompilerType element_type)
Definition: VectorType.cpp:121
static CompilerType GetCompilerTypeForFormat(lldb::Format format, CompilerType element_type, TypeSystem *type_system)
Definition: VectorType.cpp:23
llvm::Optional< uint64_t > GetByteSize(ExecutionContextScope *exe_scope) const
Return the size of the type in bytes.
bool IsVectorType(CompilerType *element_type, uint64_t *size) const
size_t ExtractIndexFromString(const char *item_name)
#define UINT32_MAX
Definition: lldb-defines.h:31
virtual CompilerType GetBasicTypeFromAST(lldb::BasicType basic_type)=0
VectorTypeSyntheticFrontEnd(lldb::ValueObjectSP valobj_sp)
Definition: VectorType.cpp:190
llvm::StringRef GetString() const
"lldb/Target/ExecutionContextScope.h" Inherit from this if your object can reconstruct its execution ...
lldb::ValueObjectSP GetChildAtIndex(size_t idx) override
Definition: VectorType.cpp:198
size_t Printf(const char *format,...) __attribute__((format(printf
Output printf formatted output to the stream.
Definition: Stream.cpp:106
virtual CompilerType GetBuiltinTypeForEncodingAndBitSize(lldb::Encoding encoding, size_t bit_size)=0
CompilerType GetPointerType() const
SyntheticChildrenFrontEnd * VectorTypeSyntheticFrontEndCreator(CXXSyntheticChildren *, lldb::ValueObjectSP)
Definition: VectorType.cpp:292
static size_t CalculateNumChildren(CompilerType container_type, CompilerType element_type, lldb_private::ExecutionContextScope *exe_scope=nullptr)
Definition: VectorType.cpp:168
size_t GetIndexOfChildWithName(ConstString name) override
Definition: VectorType.cpp:236
bool VectorTypeSummaryProvider(ValueObject &, Stream &, const TypeSummaryOptions &)
Definition: VectorType.cpp:254
size_t PutChar(char ch)
Definition: Stream.cpp:103
A uniqued constant string class.
Definition: ConstString.h:38
const char * GetCString() const
Get the string value as a C string.
Definition: ConstString.h:247
Non-standardized C, such as K&R.
Definition: SBAddress.h:15
lldb::ValueObjectSP GetSP()
Definition: ValueObject.h:565
bool IsIntegerType(bool &is_signed) const
virtual uint32_t GetPointerByteSize()=0