• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //===-- CompilerType.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 
9 #include "lldb/Symbol/CompilerType.h"
10 
11 #include "lldb/Core/Debugger.h"
12 #include "lldb/Core/StreamFile.h"
13 #include "lldb/Symbol/Type.h"
14 #include "lldb/Target/ExecutionContext.h"
15 #include "lldb/Target/Process.h"
16 #include "lldb/Utility/ConstString.h"
17 #include "lldb/Utility/DataBufferHeap.h"
18 #include "lldb/Utility/DataExtractor.h"
19 #include "lldb/Utility/Scalar.h"
20 #include "lldb/Utility/Stream.h"
21 #include "lldb/Utility/StreamString.h"
22 
23 #include <iterator>
24 #include <mutex>
25 
26 using namespace lldb;
27 using namespace lldb_private;
28 
29 // Tests
30 
IsAggregateType() const31 bool CompilerType::IsAggregateType() const {
32   if (IsValid())
33     return m_type_system->IsAggregateType(m_type);
34   return false;
35 }
36 
IsAnonymousType() const37 bool CompilerType::IsAnonymousType() const {
38   if (IsValid())
39     return m_type_system->IsAnonymousType(m_type);
40   return false;
41 }
42 
IsArrayType(CompilerType * element_type_ptr,uint64_t * size,bool * is_incomplete) const43 bool CompilerType::IsArrayType(CompilerType *element_type_ptr, uint64_t *size,
44                                bool *is_incomplete) const {
45   if (IsValid())
46     return m_type_system->IsArrayType(m_type, element_type_ptr, size,
47                                       is_incomplete);
48 
49   if (element_type_ptr)
50     element_type_ptr->Clear();
51   if (size)
52     *size = 0;
53   if (is_incomplete)
54     *is_incomplete = false;
55   return false;
56 }
57 
IsVectorType(CompilerType * element_type,uint64_t * size) const58 bool CompilerType::IsVectorType(CompilerType *element_type,
59                                 uint64_t *size) const {
60   if (IsValid())
61     return m_type_system->IsVectorType(m_type, element_type, size);
62   return false;
63 }
64 
IsRuntimeGeneratedType() const65 bool CompilerType::IsRuntimeGeneratedType() const {
66   if (IsValid())
67     return m_type_system->IsRuntimeGeneratedType(m_type);
68   return false;
69 }
70 
IsCharType() const71 bool CompilerType::IsCharType() const {
72   if (IsValid())
73     return m_type_system->IsCharType(m_type);
74   return false;
75 }
76 
IsCompleteType() const77 bool CompilerType::IsCompleteType() const {
78   if (IsValid())
79     return m_type_system->IsCompleteType(m_type);
80   return false;
81 }
82 
IsConst() const83 bool CompilerType::IsConst() const {
84   if (IsValid())
85     return m_type_system->IsConst(m_type);
86   return false;
87 }
88 
IsCStringType(uint32_t & length) const89 bool CompilerType::IsCStringType(uint32_t &length) const {
90   if (IsValid())
91     return m_type_system->IsCStringType(m_type, length);
92   return false;
93 }
94 
IsFunctionType() const95 bool CompilerType::IsFunctionType() const {
96   if (IsValid())
97     return m_type_system->IsFunctionType(m_type);
98   return false;
99 }
100 
101 // Used to detect "Homogeneous Floating-point Aggregates"
102 uint32_t
IsHomogeneousAggregate(CompilerType * base_type_ptr) const103 CompilerType::IsHomogeneousAggregate(CompilerType *base_type_ptr) const {
104   if (IsValid())
105     return m_type_system->IsHomogeneousAggregate(m_type, base_type_ptr);
106   return 0;
107 }
108 
GetNumberOfFunctionArguments() const109 size_t CompilerType::GetNumberOfFunctionArguments() const {
110   if (IsValid())
111     return m_type_system->GetNumberOfFunctionArguments(m_type);
112   return 0;
113 }
114 
115 CompilerType
GetFunctionArgumentAtIndex(const size_t index) const116 CompilerType::GetFunctionArgumentAtIndex(const size_t index) const {
117   if (IsValid())
118     return m_type_system->GetFunctionArgumentAtIndex(m_type, index);
119   return CompilerType();
120 }
121 
IsFunctionPointerType() const122 bool CompilerType::IsFunctionPointerType() const {
123   if (IsValid())
124     return m_type_system->IsFunctionPointerType(m_type);
125   return false;
126 }
127 
IsBlockPointerType(CompilerType * function_pointer_type_ptr) const128 bool CompilerType::IsBlockPointerType(
129     CompilerType *function_pointer_type_ptr) const {
130   if (IsValid())
131     return m_type_system->IsBlockPointerType(m_type, function_pointer_type_ptr);
132   return false;
133 }
134 
IsIntegerType(bool & is_signed) const135 bool CompilerType::IsIntegerType(bool &is_signed) const {
136   if (IsValid())
137     return m_type_system->IsIntegerType(m_type, is_signed);
138   return false;
139 }
140 
IsEnumerationType(bool & is_signed) const141 bool CompilerType::IsEnumerationType(bool &is_signed) const {
142   if (IsValid())
143     return m_type_system->IsEnumerationType(m_type, is_signed);
144   return false;
145 }
146 
IsIntegerOrEnumerationType(bool & is_signed) const147 bool CompilerType::IsIntegerOrEnumerationType(bool &is_signed) const {
148   return IsIntegerType(is_signed) || IsEnumerationType(is_signed);
149 }
150 
IsPointerType(CompilerType * pointee_type) const151 bool CompilerType::IsPointerType(CompilerType *pointee_type) const {
152   if (IsValid()) {
153     return m_type_system->IsPointerType(m_type, pointee_type);
154   }
155   if (pointee_type)
156     pointee_type->Clear();
157   return false;
158 }
159 
IsPointerOrReferenceType(CompilerType * pointee_type) const160 bool CompilerType::IsPointerOrReferenceType(CompilerType *pointee_type) const {
161   if (IsValid()) {
162     return m_type_system->IsPointerOrReferenceType(m_type, pointee_type);
163   }
164   if (pointee_type)
165     pointee_type->Clear();
166   return false;
167 }
168 
IsReferenceType(CompilerType * pointee_type,bool * is_rvalue) const169 bool CompilerType::IsReferenceType(CompilerType *pointee_type,
170                                    bool *is_rvalue) const {
171   if (IsValid()) {
172     return m_type_system->IsReferenceType(m_type, pointee_type, is_rvalue);
173   }
174   if (pointee_type)
175     pointee_type->Clear();
176   return false;
177 }
178 
ShouldTreatScalarValueAsAddress() const179 bool CompilerType::ShouldTreatScalarValueAsAddress() const {
180   if (IsValid())
181     return m_type_system->ShouldTreatScalarValueAsAddress(m_type);
182   return false;
183 }
184 
IsFloatingPointType(uint32_t & count,bool & is_complex) const185 bool CompilerType::IsFloatingPointType(uint32_t &count,
186                                        bool &is_complex) const {
187   if (IsValid()) {
188     return m_type_system->IsFloatingPointType(m_type, count, is_complex);
189   }
190   count = 0;
191   is_complex = false;
192   return false;
193 }
194 
IsDefined() const195 bool CompilerType::IsDefined() const {
196   if (IsValid())
197     return m_type_system->IsDefined(m_type);
198   return true;
199 }
200 
IsPolymorphicClass() const201 bool CompilerType::IsPolymorphicClass() const {
202   if (IsValid()) {
203     return m_type_system->IsPolymorphicClass(m_type);
204   }
205   return false;
206 }
207 
IsPossibleDynamicType(CompilerType * dynamic_pointee_type,bool check_cplusplus,bool check_objc) const208 bool CompilerType::IsPossibleDynamicType(CompilerType *dynamic_pointee_type,
209                                          bool check_cplusplus,
210                                          bool check_objc) const {
211   if (IsValid())
212     return m_type_system->IsPossibleDynamicType(m_type, dynamic_pointee_type,
213                                                 check_cplusplus, check_objc);
214   return false;
215 }
216 
IsScalarType() const217 bool CompilerType::IsScalarType() const {
218   if (!IsValid())
219     return false;
220 
221   return m_type_system->IsScalarType(m_type);
222 }
223 
IsTypedefType() const224 bool CompilerType::IsTypedefType() const {
225   if (!IsValid())
226     return false;
227   return m_type_system->IsTypedefType(m_type);
228 }
229 
IsVoidType() const230 bool CompilerType::IsVoidType() const {
231   if (!IsValid())
232     return false;
233   return m_type_system->IsVoidType(m_type);
234 }
235 
IsPointerToScalarType() const236 bool CompilerType::IsPointerToScalarType() const {
237   if (!IsValid())
238     return false;
239 
240   return IsPointerType() && GetPointeeType().IsScalarType();
241 }
242 
IsArrayOfScalarType() const243 bool CompilerType::IsArrayOfScalarType() const {
244   CompilerType element_type;
245   if (IsArrayType(&element_type, nullptr, nullptr))
246     return element_type.IsScalarType();
247   return false;
248 }
249 
IsBeingDefined() const250 bool CompilerType::IsBeingDefined() const {
251   if (!IsValid())
252     return false;
253   return m_type_system->IsBeingDefined(m_type);
254 }
255 
256 // Type Completion
257 
GetCompleteType() const258 bool CompilerType::GetCompleteType() const {
259   if (!IsValid())
260     return false;
261   return m_type_system->GetCompleteType(m_type);
262 }
263 
264 // AST related queries
GetPointerByteSize() const265 size_t CompilerType::GetPointerByteSize() const {
266   if (m_type_system)
267     return m_type_system->GetPointerByteSize();
268   return 0;
269 }
270 
GetTypeName() const271 ConstString CompilerType::GetTypeName() const {
272   if (IsValid()) {
273     return m_type_system->GetTypeName(m_type);
274   }
275   return ConstString("<invalid>");
276 }
277 
GetDisplayTypeName() const278 ConstString CompilerType::GetDisplayTypeName() const {
279   if (IsValid())
280     return m_type_system->GetDisplayTypeName(m_type);
281   return ConstString("<invalid>");
282 }
283 
GetTypeInfo(CompilerType * pointee_or_element_compiler_type) const284 uint32_t CompilerType::GetTypeInfo(
285     CompilerType *pointee_or_element_compiler_type) const {
286   if (!IsValid())
287     return 0;
288 
289   return m_type_system->GetTypeInfo(m_type, pointee_or_element_compiler_type);
290 }
291 
GetMinimumLanguage()292 lldb::LanguageType CompilerType::GetMinimumLanguage() {
293   if (!IsValid())
294     return lldb::eLanguageTypeC;
295 
296   return m_type_system->GetMinimumLanguage(m_type);
297 }
298 
GetTypeClass() const299 lldb::TypeClass CompilerType::GetTypeClass() const {
300   if (!IsValid())
301     return lldb::eTypeClassInvalid;
302 
303   return m_type_system->GetTypeClass(m_type);
304 }
305 
SetCompilerType(TypeSystem * type_system,lldb::opaque_compiler_type_t type)306 void CompilerType::SetCompilerType(TypeSystem *type_system,
307                                    lldb::opaque_compiler_type_t type) {
308   m_type_system = type_system;
309   m_type = type;
310 }
311 
GetTypeQualifiers() const312 unsigned CompilerType::GetTypeQualifiers() const {
313   if (IsValid())
314     return m_type_system->GetTypeQualifiers(m_type);
315   return 0;
316 }
317 
318 // Creating related types
319 
320 CompilerType
GetArrayElementType(ExecutionContextScope * exe_scope) const321 CompilerType::GetArrayElementType(ExecutionContextScope *exe_scope) const {
322   if (IsValid()) {
323     return m_type_system->GetArrayElementType(m_type, exe_scope);
324   }
325   return CompilerType();
326 }
327 
GetArrayType(uint64_t size) const328 CompilerType CompilerType::GetArrayType(uint64_t size) const {
329   if (IsValid()) {
330     return m_type_system->GetArrayType(m_type, size);
331   }
332   return CompilerType();
333 }
334 
GetCanonicalType() const335 CompilerType CompilerType::GetCanonicalType() const {
336   if (IsValid())
337     return m_type_system->GetCanonicalType(m_type);
338   return CompilerType();
339 }
340 
GetFullyUnqualifiedType() const341 CompilerType CompilerType::GetFullyUnqualifiedType() const {
342   if (IsValid())
343     return m_type_system->GetFullyUnqualifiedType(m_type);
344   return CompilerType();
345 }
346 
GetFunctionArgumentCount() const347 int CompilerType::GetFunctionArgumentCount() const {
348   if (IsValid()) {
349     return m_type_system->GetFunctionArgumentCount(m_type);
350   }
351   return -1;
352 }
353 
GetFunctionArgumentTypeAtIndex(size_t idx) const354 CompilerType CompilerType::GetFunctionArgumentTypeAtIndex(size_t idx) const {
355   if (IsValid()) {
356     return m_type_system->GetFunctionArgumentTypeAtIndex(m_type, idx);
357   }
358   return CompilerType();
359 }
360 
GetFunctionReturnType() const361 CompilerType CompilerType::GetFunctionReturnType() const {
362   if (IsValid()) {
363     return m_type_system->GetFunctionReturnType(m_type);
364   }
365   return CompilerType();
366 }
367 
GetNumMemberFunctions() const368 size_t CompilerType::GetNumMemberFunctions() const {
369   if (IsValid()) {
370     return m_type_system->GetNumMemberFunctions(m_type);
371   }
372   return 0;
373 }
374 
GetMemberFunctionAtIndex(size_t idx)375 TypeMemberFunctionImpl CompilerType::GetMemberFunctionAtIndex(size_t idx) {
376   if (IsValid()) {
377     return m_type_system->GetMemberFunctionAtIndex(m_type, idx);
378   }
379   return TypeMemberFunctionImpl();
380 }
381 
GetNonReferenceType() const382 CompilerType CompilerType::GetNonReferenceType() const {
383   if (IsValid())
384     return m_type_system->GetNonReferenceType(m_type);
385   return CompilerType();
386 }
387 
GetPointeeType() const388 CompilerType CompilerType::GetPointeeType() const {
389   if (IsValid()) {
390     return m_type_system->GetPointeeType(m_type);
391   }
392   return CompilerType();
393 }
394 
GetPointerType() const395 CompilerType CompilerType::GetPointerType() const {
396   if (IsValid()) {
397     return m_type_system->GetPointerType(m_type);
398   }
399   return CompilerType();
400 }
401 
GetLValueReferenceType() const402 CompilerType CompilerType::GetLValueReferenceType() const {
403   if (IsValid())
404     return m_type_system->GetLValueReferenceType(m_type);
405   else
406     return CompilerType();
407 }
408 
GetRValueReferenceType() const409 CompilerType CompilerType::GetRValueReferenceType() const {
410   if (IsValid())
411     return m_type_system->GetRValueReferenceType(m_type);
412   else
413     return CompilerType();
414 }
415 
GetAtomicType() const416 CompilerType CompilerType::GetAtomicType() const {
417   if (IsValid())
418     return m_type_system->GetAtomicType(m_type);
419   return CompilerType();
420 }
421 
AddConstModifier() const422 CompilerType CompilerType::AddConstModifier() const {
423   if (IsValid())
424     return m_type_system->AddConstModifier(m_type);
425   else
426     return CompilerType();
427 }
428 
AddVolatileModifier() const429 CompilerType CompilerType::AddVolatileModifier() const {
430   if (IsValid())
431     return m_type_system->AddVolatileModifier(m_type);
432   else
433     return CompilerType();
434 }
435 
AddRestrictModifier() const436 CompilerType CompilerType::AddRestrictModifier() const {
437   if (IsValid())
438     return m_type_system->AddRestrictModifier(m_type);
439   else
440     return CompilerType();
441 }
442 
CreateTypedef(const char * name,const CompilerDeclContext & decl_ctx,uint32_t payload) const443 CompilerType CompilerType::CreateTypedef(const char *name,
444                                          const CompilerDeclContext &decl_ctx,
445                                          uint32_t payload) const {
446   if (IsValid())
447     return m_type_system->CreateTypedef(m_type, name, decl_ctx, payload);
448   else
449     return CompilerType();
450 }
451 
GetTypedefedType() const452 CompilerType CompilerType::GetTypedefedType() const {
453   if (IsValid())
454     return m_type_system->GetTypedefedType(m_type);
455   else
456     return CompilerType();
457 }
458 
459 // Create related types using the current type's AST
460 
461 CompilerType
GetBasicTypeFromAST(lldb::BasicType basic_type) const462 CompilerType::GetBasicTypeFromAST(lldb::BasicType basic_type) const {
463   if (IsValid())
464     return m_type_system->GetBasicTypeFromAST(basic_type);
465   return CompilerType();
466 }
467 // Exploring the type
468 
469 llvm::Optional<uint64_t>
GetBitSize(ExecutionContextScope * exe_scope) const470 CompilerType::GetBitSize(ExecutionContextScope *exe_scope) const {
471   if (IsValid())
472     return m_type_system->GetBitSize(m_type, exe_scope);
473   return {};
474 }
475 
476 llvm::Optional<uint64_t>
GetByteSize(ExecutionContextScope * exe_scope) const477 CompilerType::GetByteSize(ExecutionContextScope *exe_scope) const {
478   if (llvm::Optional<uint64_t> bit_size = GetBitSize(exe_scope))
479     return (*bit_size + 7) / 8;
480   return {};
481 }
482 
GetTypeBitAlign(ExecutionContextScope * exe_scope) const483 llvm::Optional<size_t> CompilerType::GetTypeBitAlign(ExecutionContextScope *exe_scope) const {
484   if (IsValid())
485     return m_type_system->GetTypeBitAlign(m_type, exe_scope);
486   return {};
487 }
488 
GetEncoding(uint64_t & count) const489 lldb::Encoding CompilerType::GetEncoding(uint64_t &count) const {
490   if (!IsValid())
491     return lldb::eEncodingInvalid;
492 
493   return m_type_system->GetEncoding(m_type, count);
494 }
495 
GetFormat() const496 lldb::Format CompilerType::GetFormat() const {
497   if (!IsValid())
498     return lldb::eFormatDefault;
499 
500   return m_type_system->GetFormat(m_type);
501 }
502 
GetNumChildren(bool omit_empty_base_classes,const ExecutionContext * exe_ctx) const503 uint32_t CompilerType::GetNumChildren(bool omit_empty_base_classes,
504                                       const ExecutionContext *exe_ctx) const {
505   if (!IsValid())
506     return 0;
507   return m_type_system->GetNumChildren(m_type, omit_empty_base_classes,
508                                        exe_ctx);
509 }
510 
GetBasicTypeEnumeration() const511 lldb::BasicType CompilerType::GetBasicTypeEnumeration() const {
512   if (IsValid())
513     return m_type_system->GetBasicTypeEnumeration(m_type);
514   return eBasicTypeInvalid;
515 }
516 
ForEachEnumerator(std::function<bool (const CompilerType & integer_type,ConstString name,const llvm::APSInt & value)> const & callback) const517 void CompilerType::ForEachEnumerator(
518     std::function<bool(const CompilerType &integer_type,
519                        ConstString name,
520                        const llvm::APSInt &value)> const &callback) const {
521   if (IsValid())
522     return m_type_system->ForEachEnumerator(m_type, callback);
523 }
524 
GetNumFields() const525 uint32_t CompilerType::GetNumFields() const {
526   if (!IsValid())
527     return 0;
528   return m_type_system->GetNumFields(m_type);
529 }
530 
GetFieldAtIndex(size_t idx,std::string & name,uint64_t * bit_offset_ptr,uint32_t * bitfield_bit_size_ptr,bool * is_bitfield_ptr) const531 CompilerType CompilerType::GetFieldAtIndex(size_t idx, std::string &name,
532                                            uint64_t *bit_offset_ptr,
533                                            uint32_t *bitfield_bit_size_ptr,
534                                            bool *is_bitfield_ptr) const {
535   if (!IsValid())
536     return CompilerType();
537   return m_type_system->GetFieldAtIndex(m_type, idx, name, bit_offset_ptr,
538                                         bitfield_bit_size_ptr, is_bitfield_ptr);
539 }
540 
GetNumDirectBaseClasses() const541 uint32_t CompilerType::GetNumDirectBaseClasses() const {
542   if (IsValid())
543     return m_type_system->GetNumDirectBaseClasses(m_type);
544   return 0;
545 }
546 
GetNumVirtualBaseClasses() const547 uint32_t CompilerType::GetNumVirtualBaseClasses() const {
548   if (IsValid())
549     return m_type_system->GetNumVirtualBaseClasses(m_type);
550   return 0;
551 }
552 
553 CompilerType
GetDirectBaseClassAtIndex(size_t idx,uint32_t * bit_offset_ptr) const554 CompilerType::GetDirectBaseClassAtIndex(size_t idx,
555                                         uint32_t *bit_offset_ptr) const {
556   if (IsValid())
557     return m_type_system->GetDirectBaseClassAtIndex(m_type, idx,
558                                                     bit_offset_ptr);
559   return CompilerType();
560 }
561 
562 CompilerType
GetVirtualBaseClassAtIndex(size_t idx,uint32_t * bit_offset_ptr) const563 CompilerType::GetVirtualBaseClassAtIndex(size_t idx,
564                                          uint32_t *bit_offset_ptr) const {
565   if (IsValid())
566     return m_type_system->GetVirtualBaseClassAtIndex(m_type, idx,
567                                                      bit_offset_ptr);
568   return CompilerType();
569 }
570 
GetIndexOfFieldWithName(const char * name,CompilerType * field_compiler_type_ptr,uint64_t * bit_offset_ptr,uint32_t * bitfield_bit_size_ptr,bool * is_bitfield_ptr) const571 uint32_t CompilerType::GetIndexOfFieldWithName(
572     const char *name, CompilerType *field_compiler_type_ptr,
573     uint64_t *bit_offset_ptr, uint32_t *bitfield_bit_size_ptr,
574     bool *is_bitfield_ptr) const {
575   unsigned count = GetNumFields();
576   std::string field_name;
577   for (unsigned index = 0; index < count; index++) {
578     CompilerType field_compiler_type(
579         GetFieldAtIndex(index, field_name, bit_offset_ptr,
580                         bitfield_bit_size_ptr, is_bitfield_ptr));
581     if (strcmp(field_name.c_str(), name) == 0) {
582       if (field_compiler_type_ptr)
583         *field_compiler_type_ptr = field_compiler_type;
584       return index;
585     }
586   }
587   return UINT32_MAX;
588 }
589 
GetChildCompilerTypeAtIndex(ExecutionContext * exe_ctx,size_t idx,bool transparent_pointers,bool omit_empty_base_classes,bool ignore_array_bounds,std::string & child_name,uint32_t & child_byte_size,int32_t & child_byte_offset,uint32_t & child_bitfield_bit_size,uint32_t & child_bitfield_bit_offset,bool & child_is_base_class,bool & child_is_deref_of_parent,ValueObject * valobj,uint64_t & language_flags) const590 CompilerType CompilerType::GetChildCompilerTypeAtIndex(
591     ExecutionContext *exe_ctx, size_t idx, bool transparent_pointers,
592     bool omit_empty_base_classes, bool ignore_array_bounds,
593     std::string &child_name, uint32_t &child_byte_size,
594     int32_t &child_byte_offset, uint32_t &child_bitfield_bit_size,
595     uint32_t &child_bitfield_bit_offset, bool &child_is_base_class,
596     bool &child_is_deref_of_parent, ValueObject *valobj,
597     uint64_t &language_flags) const {
598   if (!IsValid())
599     return CompilerType();
600   return m_type_system->GetChildCompilerTypeAtIndex(
601       m_type, exe_ctx, idx, transparent_pointers, omit_empty_base_classes,
602       ignore_array_bounds, child_name, child_byte_size, child_byte_offset,
603       child_bitfield_bit_size, child_bitfield_bit_offset, child_is_base_class,
604       child_is_deref_of_parent, valobj, language_flags);
605 }
606 
607 // Look for a child member (doesn't include base classes, but it does include
608 // their members) in the type hierarchy. Returns an index path into
609 // "clang_type" on how to reach the appropriate member.
610 //
611 //    class A
612 //    {
613 //    public:
614 //        int m_a;
615 //        int m_b;
616 //    };
617 //
618 //    class B
619 //    {
620 //    };
621 //
622 //    class C :
623 //        public B,
624 //        public A
625 //    {
626 //    };
627 //
628 // If we have a clang type that describes "class C", and we wanted to looked
629 // "m_b" in it:
630 //
631 // With omit_empty_base_classes == false we would get an integer array back
632 // with: { 1,  1 } The first index 1 is the child index for "class A" within
633 // class C The second index 1 is the child index for "m_b" within class A
634 //
635 // With omit_empty_base_classes == true we would get an integer array back
636 // with: { 0,  1 } The first index 0 is the child index for "class A" within
637 // class C (since class B doesn't have any members it doesn't count) The second
638 // index 1 is the child index for "m_b" within class A
639 
GetIndexOfChildMemberWithName(const char * name,bool omit_empty_base_classes,std::vector<uint32_t> & child_indexes) const640 size_t CompilerType::GetIndexOfChildMemberWithName(
641     const char *name, bool omit_empty_base_classes,
642     std::vector<uint32_t> &child_indexes) const {
643   if (IsValid() && name && name[0]) {
644     return m_type_system->GetIndexOfChildMemberWithName(
645         m_type, name, omit_empty_base_classes, child_indexes);
646   }
647   return 0;
648 }
649 
GetNumTemplateArguments() const650 size_t CompilerType::GetNumTemplateArguments() const {
651   if (IsValid()) {
652     return m_type_system->GetNumTemplateArguments(m_type);
653   }
654   return 0;
655 }
656 
GetTemplateArgumentKind(size_t idx) const657 TemplateArgumentKind CompilerType::GetTemplateArgumentKind(size_t idx) const {
658   if (IsValid())
659     return m_type_system->GetTemplateArgumentKind(m_type, idx);
660   return eTemplateArgumentKindNull;
661 }
662 
GetTypeTemplateArgument(size_t idx) const663 CompilerType CompilerType::GetTypeTemplateArgument(size_t idx) const {
664   if (IsValid()) {
665     return m_type_system->GetTypeTemplateArgument(m_type, idx);
666   }
667   return CompilerType();
668 }
669 
670 llvm::Optional<CompilerType::IntegralTemplateArgument>
GetIntegralTemplateArgument(size_t idx) const671 CompilerType::GetIntegralTemplateArgument(size_t idx) const {
672   if (IsValid())
673     return m_type_system->GetIntegralTemplateArgument(m_type, idx);
674   return llvm::None;
675 }
676 
GetTypeForFormatters() const677 CompilerType CompilerType::GetTypeForFormatters() const {
678   if (IsValid())
679     return m_type_system->GetTypeForFormatters(m_type);
680   return CompilerType();
681 }
682 
ShouldPrintAsOneLiner(ValueObject * valobj) const683 LazyBool CompilerType::ShouldPrintAsOneLiner(ValueObject *valobj) const {
684   if (IsValid())
685     return m_type_system->ShouldPrintAsOneLiner(m_type, valobj);
686   return eLazyBoolCalculate;
687 }
688 
IsMeaninglessWithoutDynamicResolution() const689 bool CompilerType::IsMeaninglessWithoutDynamicResolution() const {
690   if (IsValid())
691     return m_type_system->IsMeaninglessWithoutDynamicResolution(m_type);
692   return false;
693 }
694 
695 // Get the index of the child of "clang_type" whose name matches. This function
696 // doesn't descend into the children, but only looks one level deep and name
697 // matches can include base class names.
698 
699 uint32_t
GetIndexOfChildWithName(const char * name,bool omit_empty_base_classes) const700 CompilerType::GetIndexOfChildWithName(const char *name,
701                                       bool omit_empty_base_classes) const {
702   if (IsValid() && name && name[0]) {
703     return m_type_system->GetIndexOfChildWithName(m_type, name,
704                                                   omit_empty_base_classes);
705   }
706   return UINT32_MAX;
707 }
708 
709 // Dumping types
710 
DumpValue(ExecutionContext * exe_ctx,Stream * s,lldb::Format format,const DataExtractor & data,lldb::offset_t data_byte_offset,size_t data_byte_size,uint32_t bitfield_bit_size,uint32_t bitfield_bit_offset,bool show_types,bool show_summary,bool verbose,uint32_t depth)711 void CompilerType::DumpValue(ExecutionContext *exe_ctx, Stream *s,
712                              lldb::Format format, const DataExtractor &data,
713                              lldb::offset_t data_byte_offset,
714                              size_t data_byte_size, uint32_t bitfield_bit_size,
715                              uint32_t bitfield_bit_offset, bool show_types,
716                              bool show_summary, bool verbose, uint32_t depth) {
717   if (!IsValid())
718     return;
719   m_type_system->DumpValue(m_type, exe_ctx, s, format, data, data_byte_offset,
720                            data_byte_size, bitfield_bit_size,
721                            bitfield_bit_offset, show_types, show_summary,
722                            verbose, depth);
723 }
724 
DumpTypeValue(Stream * s,lldb::Format format,const DataExtractor & data,lldb::offset_t byte_offset,size_t byte_size,uint32_t bitfield_bit_size,uint32_t bitfield_bit_offset,ExecutionContextScope * exe_scope)725 bool CompilerType::DumpTypeValue(Stream *s, lldb::Format format,
726                                  const DataExtractor &data,
727                                  lldb::offset_t byte_offset, size_t byte_size,
728                                  uint32_t bitfield_bit_size,
729                                  uint32_t bitfield_bit_offset,
730                                  ExecutionContextScope *exe_scope) {
731   if (!IsValid())
732     return false;
733   return m_type_system->DumpTypeValue(m_type, s, format, data, byte_offset,
734                                       byte_size, bitfield_bit_size,
735                                       bitfield_bit_offset, exe_scope);
736 }
737 
DumpSummary(ExecutionContext * exe_ctx,Stream * s,const DataExtractor & data,lldb::offset_t data_byte_offset,size_t data_byte_size)738 void CompilerType::DumpSummary(ExecutionContext *exe_ctx, Stream *s,
739                                const DataExtractor &data,
740                                lldb::offset_t data_byte_offset,
741                                size_t data_byte_size) {
742   if (IsValid())
743     m_type_system->DumpSummary(m_type, exe_ctx, s, data, data_byte_offset,
744                                data_byte_size);
745 }
746 
DumpTypeDescription(lldb::DescriptionLevel level) const747 void CompilerType::DumpTypeDescription(lldb::DescriptionLevel level) const {
748   if (IsValid())
749     m_type_system->DumpTypeDescription(m_type, level);
750 }
751 
DumpTypeDescription(Stream * s,lldb::DescriptionLevel level) const752 void CompilerType::DumpTypeDescription(Stream *s,
753                                        lldb::DescriptionLevel level) const {
754   if (IsValid()) {
755     m_type_system->DumpTypeDescription(m_type, s, level);
756   }
757 }
758 
759 #ifndef NDEBUG
dump() const760 LLVM_DUMP_METHOD void CompilerType::dump() const {
761   if (IsValid())
762     m_type_system->dump(m_type);
763   else
764     llvm::errs() << "<invalid>\n";
765 }
766 #endif
767 
GetValueAsScalar(const lldb_private::DataExtractor & data,lldb::offset_t data_byte_offset,size_t data_byte_size,Scalar & value,ExecutionContextScope * exe_scope) const768 bool CompilerType::GetValueAsScalar(const lldb_private::DataExtractor &data,
769                                     lldb::offset_t data_byte_offset,
770                                     size_t data_byte_size, Scalar &value,
771                                     ExecutionContextScope *exe_scope) const {
772   if (!IsValid())
773     return false;
774 
775   if (IsAggregateType()) {
776     return false; // Aggregate types don't have scalar values
777   } else {
778     uint64_t count = 0;
779     lldb::Encoding encoding = GetEncoding(count);
780 
781     if (encoding == lldb::eEncodingInvalid || count != 1)
782       return false;
783 
784     llvm::Optional<uint64_t> byte_size = GetByteSize(exe_scope);
785     if (!byte_size)
786       return false;
787     lldb::offset_t offset = data_byte_offset;
788     switch (encoding) {
789     case lldb::eEncodingInvalid:
790       break;
791     case lldb::eEncodingVector:
792       break;
793     case lldb::eEncodingUint:
794       if (*byte_size <= sizeof(unsigned long long)) {
795         uint64_t uval64 = data.GetMaxU64(&offset, *byte_size);
796         if (*byte_size <= sizeof(unsigned int)) {
797           value = (unsigned int)uval64;
798           return true;
799         } else if (*byte_size <= sizeof(unsigned long)) {
800           value = (unsigned long)uval64;
801           return true;
802         } else if (*byte_size <= sizeof(unsigned long long)) {
803           value = (unsigned long long)uval64;
804           return true;
805         } else
806           value.Clear();
807       }
808       break;
809 
810     case lldb::eEncodingSint:
811       if (*byte_size <= sizeof(long long)) {
812         int64_t sval64 = data.GetMaxS64(&offset, *byte_size);
813         if (*byte_size <= sizeof(int)) {
814           value = (int)sval64;
815           return true;
816         } else if (*byte_size <= sizeof(long)) {
817           value = (long)sval64;
818           return true;
819         } else if (*byte_size <= sizeof(long long)) {
820           value = (long long)sval64;
821           return true;
822         } else
823           value.Clear();
824       }
825       break;
826 
827     case lldb::eEncodingIEEE754:
828       if (*byte_size <= sizeof(long double)) {
829         uint32_t u32;
830         uint64_t u64;
831         if (*byte_size == sizeof(float)) {
832           if (sizeof(float) == sizeof(uint32_t)) {
833             u32 = data.GetU32(&offset);
834             value = *((float *)&u32);
835             return true;
836           } else if (sizeof(float) == sizeof(uint64_t)) {
837             u64 = data.GetU64(&offset);
838             value = *((float *)&u64);
839             return true;
840           }
841         } else if (*byte_size == sizeof(double)) {
842           if (sizeof(double) == sizeof(uint32_t)) {
843             u32 = data.GetU32(&offset);
844             value = *((double *)&u32);
845             return true;
846           } else if (sizeof(double) == sizeof(uint64_t)) {
847             u64 = data.GetU64(&offset);
848             value = *((double *)&u64);
849             return true;
850           }
851         } else if (*byte_size == sizeof(long double)) {
852           if (sizeof(long double) == sizeof(uint32_t)) {
853             u32 = data.GetU32(&offset);
854             value = *((long double *)&u32);
855             return true;
856           } else if (sizeof(long double) == sizeof(uint64_t)) {
857             u64 = data.GetU64(&offset);
858             value = *((long double *)&u64);
859             return true;
860           }
861         }
862       }
863       break;
864     }
865   }
866   return false;
867 }
868 
869 #ifndef NDEBUG
Verify() const870 bool CompilerType::Verify() const {
871   return !IsValid() || m_type_system->Verify(m_type);
872 }
873 #endif
874 
operator ==(const lldb_private::CompilerType & lhs,const lldb_private::CompilerType & rhs)875 bool lldb_private::operator==(const lldb_private::CompilerType &lhs,
876                               const lldb_private::CompilerType &rhs) {
877   return lhs.GetTypeSystem() == rhs.GetTypeSystem() &&
878          lhs.GetOpaqueQualType() == rhs.GetOpaqueQualType();
879 }
880 
operator !=(const lldb_private::CompilerType & lhs,const lldb_private::CompilerType & rhs)881 bool lldb_private::operator!=(const lldb_private::CompilerType &lhs,
882                               const lldb_private::CompilerType &rhs) {
883   return !(lhs == rhs);
884 }
885