]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/llvm/tools/lldb/source/DataFormatters/VectorType.cpp
Merge ^/head r303250 through r308226.
[FreeBSD/FreeBSD.git] / contrib / llvm / tools / lldb / source / DataFormatters / VectorType.cpp
1 //===-- VectorType.cpp ------------------------------------------*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9
10 // C Includes
11 // C++ Includes
12 // Other libraries and framework includes
13 // Project includes
14 #include "lldb/DataFormatters/VectorType.h"
15
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"
21
22 #include "lldb/Utility/LLDBAssert.h"
23
24 using namespace lldb;
25 using namespace lldb_private;
26 using namespace lldb_private::formatters;
27
28 static CompilerType
29 GetCompilerTypeForFormat (lldb::Format format,
30                           CompilerType element_type,
31                           TypeSystem *type_system)
32 {
33     lldbassert(type_system && "type_system needs to be not NULL");
34     
35     switch (format)
36     {
37         case lldb::eFormatAddressInfo:
38         case lldb::eFormatPointer:
39             return type_system->GetBuiltinTypeForEncodingAndBitSize (eEncodingUint, 8*type_system->GetPointerByteSize());
40             
41         case lldb::eFormatBoolean:
42             return type_system->GetBasicTypeFromAST(lldb::eBasicTypeBool);
43             
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);
50
51         case lldb::eFormatComplex /* lldb::eFormatComplexFloat */:
52             return type_system->GetBasicTypeFromAST(lldb::eBasicTypeFloatComplex);
53
54         case lldb::eFormatCString:
55             return type_system->GetBasicTypeFromAST(lldb::eBasicTypeChar).GetPointerType();
56
57         case lldb::eFormatFloat:
58             return type_system->GetBasicTypeFromAST(lldb::eBasicTypeFloat);
59             
60         case lldb::eFormatHex:
61         case lldb::eFormatHexUppercase:
62         case lldb::eFormatOctal:
63             return type_system->GetBasicTypeFromAST(lldb::eBasicTypeInt);
64
65         case lldb::eFormatHexFloat:
66             return type_system->GetBasicTypeFromAST(lldb::eBasicTypeFloat);
67
68         case lldb::eFormatUnicode16:
69         case lldb::eFormatUnicode32:
70
71         case lldb::eFormatUnsigned:
72             return type_system->GetBasicTypeFromAST(lldb::eBasicTypeUnsignedInt);
73
74         case lldb::eFormatVectorOfChar:
75             return type_system->GetBasicTypeFromAST(lldb::eBasicTypeChar);
76             
77         case lldb::eFormatVectorOfFloat32:
78             return type_system->GetBuiltinTypeForEncodingAndBitSize(eEncodingIEEE754, 32);
79             
80         case lldb::eFormatVectorOfFloat64:
81             return type_system->GetBuiltinTypeForEncodingAndBitSize(eEncodingIEEE754, 64);
82             
83         case lldb::eFormatVectorOfSInt16:
84             return type_system->GetBuiltinTypeForEncodingAndBitSize (eEncodingSint, 16);
85             
86         case lldb::eFormatVectorOfSInt32:
87             return type_system->GetBuiltinTypeForEncodingAndBitSize (eEncodingSint, 32);
88
89         case lldb::eFormatVectorOfSInt64:
90             return type_system->GetBuiltinTypeForEncodingAndBitSize (eEncodingSint, 64);
91             
92         case lldb::eFormatVectorOfSInt8:
93             return type_system->GetBuiltinTypeForEncodingAndBitSize (eEncodingSint, 8);
94
95         case lldb::eFormatVectorOfUInt128:
96             return type_system->GetBuiltinTypeForEncodingAndBitSize (eEncodingUint, 128);
97
98         case lldb::eFormatVectorOfUInt16:
99             return type_system->GetBuiltinTypeForEncodingAndBitSize (eEncodingUint, 16);
100
101         case lldb::eFormatVectorOfUInt32:
102             return type_system->GetBuiltinTypeForEncodingAndBitSize (eEncodingUint, 32);
103
104         case lldb::eFormatVectorOfUInt64:
105             return type_system->GetBuiltinTypeForEncodingAndBitSize (eEncodingUint, 64);
106
107         case lldb::eFormatVectorOfUInt8:
108             return type_system->GetBuiltinTypeForEncodingAndBitSize (eEncodingUint, 8);
109             
110         case lldb::eFormatDefault:
111             return element_type;
112         
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:
120         default:
121             return type_system->GetBuiltinTypeForEncodingAndBitSize (eEncodingUint, 8);
122     }
123 }
124
125 static lldb::Format
126 GetItemFormatForFormat (lldb::Format format,
127                         CompilerType element_type)
128 {
129     switch (format)
130     {
131         case lldb::eFormatVectorOfChar:
132             return lldb::eFormatChar;
133             
134         case lldb::eFormatVectorOfFloat32:
135         case lldb::eFormatVectorOfFloat64:
136             return lldb::eFormatFloat;
137             
138         case lldb::eFormatVectorOfSInt16:
139         case lldb::eFormatVectorOfSInt32:
140         case lldb::eFormatVectorOfSInt64:
141         case lldb::eFormatVectorOfSInt8:
142             return lldb::eFormatDecimal;
143             
144         case lldb::eFormatVectorOfUInt128:
145         case lldb::eFormatVectorOfUInt16:
146         case lldb::eFormatVectorOfUInt32:
147         case lldb::eFormatVectorOfUInt64:
148         case lldb::eFormatVectorOfUInt8:
149             return lldb::eFormatUnsigned;
150             
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:
158             return eFormatHex;
159
160         case lldb::eFormatDefault:
161         {
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;
168         }
169             break;
170             
171         default:
172             return format;
173     }
174 }
175
176 static size_t
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
180                       )
181 {
182     auto container_size = container_type.GetByteSize(exe_scope);
183     auto element_size = element_type.GetByteSize(exe_scope);
184     
185     if (element_size)
186     {
187         if (container_size % element_size)
188             return 0;
189         return container_size / element_size;
190     }
191     return 0;
192 }
193
194 namespace lldb_private {
195     namespace formatters {
196
197         class VectorTypeSyntheticFrontEnd : public SyntheticChildrenFrontEnd
198         {
199         public:
200             VectorTypeSyntheticFrontEnd (lldb::ValueObjectSP valobj_sp) :
201             SyntheticChildrenFrontEnd(*valobj_sp),
202             m_parent_format (eFormatInvalid),
203             m_item_format(eFormatInvalid),
204             m_child_type(),
205             m_num_children(0)
206             {}
207
208             ~VectorTypeSyntheticFrontEnd() override = default;
209
210             size_t
211             CalculateNumChildren() override
212             {
213                 return m_num_children;
214             }
215
216             lldb::ValueObjectSP
217             GetChildAtIndex(size_t idx) override
218             {
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));
223                 if (!child_sp)
224                     return child_sp;
225                 
226                 StreamString idx_name;
227                 idx_name.Printf("[%" PRIu64 "]", (uint64_t)idx);
228                 child_sp->SetName( ConstString( idx_name.GetData() ) );
229                 
230                 child_sp->SetFormat(m_item_format);
231                 
232                 return child_sp;
233             }
234
235             bool
236             Update() override
237             {
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,
244                                                           element_type,
245                                                           target_sp ? target_sp->GetScratchTypeSystemForLanguage(nullptr, lldb::eLanguageTypeC) : nullptr);
246                 m_num_children = ::CalculateNumChildren(parent_type,
247                                                         m_child_type);
248                 m_item_format = GetItemFormatForFormat(m_parent_format,
249                                                        m_child_type);
250                 return false;
251             }
252
253             bool
254             MightHaveChildren() override
255             {
256                 return true;
257             }
258
259             size_t
260             GetIndexOfChildWithName(const ConstString &name) override
261             {
262                 const char* item_name = name.GetCString();
263                 uint32_t idx = ExtractIndexFromString(item_name);
264                 if (idx < UINT32_MAX && idx >= CalculateNumChildren())
265                     return UINT32_MAX;
266                 return idx;
267             }
268
269         private:
270             lldb::Format m_parent_format;
271             lldb::Format m_item_format;
272             CompilerType m_child_type;
273             size_t m_num_children;
274         };
275
276     } // namespace formatters
277 } // namespace lldb_private
278
279 bool
280 lldb_private::formatters::VectorTypeSummaryProvider (ValueObject& valobj,
281                                                      Stream& s,
282                                                      const TypeSummaryOptions&)
283 {
284     auto synthetic_children = VectorTypeSyntheticFrontEndCreator(nullptr, valobj.GetSP());
285     if (!synthetic_children)
286         return false;
287     
288     synthetic_children->Update();
289     
290     s.PutChar('(');
291     bool first = true;
292     
293     size_t idx = 0, len = synthetic_children->CalculateNumChildren();
294     
295     for (;
296          idx < len;
297          idx++)
298     {
299         auto child_sp = synthetic_children->GetChildAtIndex(idx);
300         if (!child_sp)
301             continue;
302         child_sp = child_sp->GetQualifiedRepresentationIfAvailable(lldb::eDynamicDontRunTarget, true);
303         
304         const char* child_value = child_sp->GetValueAsCString();
305         if (child_value && *child_value)
306         {
307             if (first)
308             {
309                 s.Printf("%s", child_value);
310                 first = false;
311             }
312             else
313             {
314                 s.Printf(", %s", child_value);
315             }
316         }
317     }
318     
319     s.PutChar(')');
320     
321     return true;
322 }
323
324 lldb_private::SyntheticChildrenFrontEnd*
325 lldb_private::formatters::VectorTypeSyntheticFrontEndCreator (CXXSyntheticChildren*, lldb::ValueObjectSP valobj_sp)
326 {
327     if (!valobj_sp)
328         return nullptr;
329     return new VectorTypeSyntheticFrontEnd(valobj_sp);
330 }