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 
IsScopedEnumerationType() const43 bool CompilerType::IsScopedEnumerationType() const {
44   if (IsValid())
45     return m_type_system->IsScopedEnumerationType(m_type);
46   return false;
47 }
48 
IsArrayType(CompilerType * element_type_ptr,uint64_t * size,bool * is_incomplete) const49 bool CompilerType::IsArrayType(CompilerType *element_type_ptr, uint64_t *size,
50                                bool *is_incomplete) const {
51   if (IsValid())
52     return m_type_system->IsArrayType(m_type, element_type_ptr, size,
53                                       is_incomplete);
54 
55   if (element_type_ptr)
56     element_type_ptr->Clear();
57   if (size)
58     *size = 0;
59   if (is_incomplete)
60     *is_incomplete = false;
61   return false;
62 }
63 
IsVectorType(CompilerType * element_type,uint64_t * size) const64 bool CompilerType::IsVectorType(CompilerType *element_type,
65                                 uint64_t *size) const {
66   if (IsValid())
67     return m_type_system->IsVectorType(m_type, element_type, size);
68   return false;
69 }
70 
IsRuntimeGeneratedType() const71 bool CompilerType::IsRuntimeGeneratedType() const {
72   if (IsValid())
73     return m_type_system->IsRuntimeGeneratedType(m_type);
74   return false;
75 }
76 
IsCharType() const77 bool CompilerType::IsCharType() const {
78   if (IsValid())
79     return m_type_system->IsCharType(m_type);
80   return false;
81 }
82 
IsCompleteType() const83 bool CompilerType::IsCompleteType() const {
84   if (IsValid())
85     return m_type_system->IsCompleteType(m_type);
86   return false;
87 }
88 
IsConst() const89 bool CompilerType::IsConst() const {
90   if (IsValid())
91     return m_type_system->IsConst(m_type);
92   return false;
93 }
94 
IsCStringType(uint32_t & length) const95 bool CompilerType::IsCStringType(uint32_t &length) const {
96   if (IsValid())
97     return m_type_system->IsCStringType(m_type, length);
98   return false;
99 }
100 
IsFunctionType() const101 bool CompilerType::IsFunctionType() const {
102   if (IsValid())
103     return m_type_system->IsFunctionType(m_type);
104   return false;
105 }
106 
107 // Used to detect "Homogeneous Floating-point Aggregates"
108 uint32_t
IsHomogeneousAggregate(CompilerType * base_type_ptr) const109 CompilerType::IsHomogeneousAggregate(CompilerType *base_type_ptr) const {
110   if (IsValid())
111     return m_type_system->IsHomogeneousAggregate(m_type, base_type_ptr);
112   return 0;
113 }
114 
GetNumberOfFunctionArguments() const115 size_t CompilerType::GetNumberOfFunctionArguments() const {
116   if (IsValid())
117     return m_type_system->GetNumberOfFunctionArguments(m_type);
118   return 0;
119 }
120 
121 CompilerType
GetFunctionArgumentAtIndex(const size_t index) const122 CompilerType::GetFunctionArgumentAtIndex(const size_t index) const {
123   if (IsValid())
124     return m_type_system->GetFunctionArgumentAtIndex(m_type, index);
125   return CompilerType();
126 }
127 
IsFunctionPointerType() const128 bool CompilerType::IsFunctionPointerType() const {
129   if (IsValid())
130     return m_type_system->IsFunctionPointerType(m_type);
131   return false;
132 }
133 
IsBlockPointerType(CompilerType * function_pointer_type_ptr) const134 bool CompilerType::IsBlockPointerType(
135     CompilerType *function_pointer_type_ptr) const {
136   if (IsValid())
137     return m_type_system->IsBlockPointerType(m_type, function_pointer_type_ptr);
138   return false;
139 }
140 
IsIntegerType(bool & is_signed) const141 bool CompilerType::IsIntegerType(bool &is_signed) const {
142   if (IsValid())
143     return m_type_system->IsIntegerType(m_type, is_signed);
144   return false;
145 }
146 
IsEnumerationType(bool & is_signed) const147 bool CompilerType::IsEnumerationType(bool &is_signed) const {
148   if (IsValid())
149     return m_type_system->IsEnumerationType(m_type, is_signed);
150   return false;
151 }
152 
IsIntegerOrEnumerationType(bool & is_signed) const153 bool CompilerType::IsIntegerOrEnumerationType(bool &is_signed) const {
154   return IsIntegerType(is_signed) || IsEnumerationType(is_signed);
155 }
156 
IsPointerType(CompilerType * pointee_type) const157 bool CompilerType::IsPointerType(CompilerType *pointee_type) const {
158   if (IsValid()) {
159     return m_type_system->IsPointerType(m_type, pointee_type);
160   }
161   if (pointee_type)
162     pointee_type->Clear();
163   return false;
164 }
165 
IsPointerOrReferenceType(CompilerType * pointee_type) const166 bool CompilerType::IsPointerOrReferenceType(CompilerType *pointee_type) const {
167   if (IsValid()) {
168     return m_type_system->IsPointerOrReferenceType(m_type, pointee_type);
169   }
170   if (pointee_type)
171     pointee_type->Clear();
172   return false;
173 }
174 
IsReferenceType(CompilerType * pointee_type,bool * is_rvalue) const175 bool CompilerType::IsReferenceType(CompilerType *pointee_type,
176                                    bool *is_rvalue) const {
177   if (IsValid()) {
178     return m_type_system->IsReferenceType(m_type, pointee_type, is_rvalue);
179   }
180   if (pointee_type)
181     pointee_type->Clear();
182   return false;
183 }
184 
ShouldTreatScalarValueAsAddress() const185 bool CompilerType::ShouldTreatScalarValueAsAddress() const {
186   if (IsValid())
187     return m_type_system->ShouldTreatScalarValueAsAddress(m_type);
188   return false;
189 }
190 
IsFloatingPointType(uint32_t & count,bool & is_complex) const191 bool CompilerType::IsFloatingPointType(uint32_t &count,
192                                        bool &is_complex) const {
193   if (IsValid()) {
194     return m_type_system->IsFloatingPointType(m_type, count, is_complex);
195   }
196   count = 0;
197   is_complex = false;
198   return false;
199 }
200 
IsDefined() const201 bool CompilerType::IsDefined() const {
202   if (IsValid())
203     return m_type_system->IsDefined(m_type);
204   return true;
205 }
206 
IsPolymorphicClass() const207 bool CompilerType::IsPolymorphicClass() const {
208   if (IsValid()) {
209     return m_type_system->IsPolymorphicClass(m_type);
210   }
211   return false;
212 }
213 
IsPossibleDynamicType(CompilerType * dynamic_pointee_type,bool check_cplusplus,bool check_objc) const214 bool CompilerType::IsPossibleDynamicType(CompilerType *dynamic_pointee_type,
215                                          bool check_cplusplus,
216                                          bool check_objc) const {
217   if (IsValid())
218     return m_type_system->IsPossibleDynamicType(m_type, dynamic_pointee_type,
219                                                 check_cplusplus, check_objc);
220   return false;
221 }
222 
IsScalarType() const223 bool CompilerType::IsScalarType() const {
224   if (!IsValid())
225     return false;
226 
227   return m_type_system->IsScalarType(m_type);
228 }
229 
IsTypedefType() const230 bool CompilerType::IsTypedefType() const {
231   if (!IsValid())
232     return false;
233   return m_type_system->IsTypedefType(m_type);
234 }
235 
IsVoidType() const236 bool CompilerType::IsVoidType() const {
237   if (!IsValid())
238     return false;
239   return m_type_system->IsVoidType(m_type);
240 }
241 
IsPointerToScalarType() const242 bool CompilerType::IsPointerToScalarType() const {
243   if (!IsValid())
244     return false;
245 
246   return IsPointerType() && GetPointeeType().IsScalarType();
247 }
248 
IsArrayOfScalarType() const249 bool CompilerType::IsArrayOfScalarType() const {
250   CompilerType element_type;
251   if (IsArrayType(&element_type))
252     return element_type.IsScalarType();
253   return false;
254 }
255 
IsBeingDefined() const256 bool CompilerType::IsBeingDefined() const {
257   if (!IsValid())
258     return false;
259   return m_type_system->IsBeingDefined(m_type);
260 }
261 
262 // Type Completion
263 
GetCompleteType() const264 bool CompilerType::GetCompleteType() const {
265   if (!IsValid())
266     return false;
267   return m_type_system->GetCompleteType(m_type);
268 }
269 
270 // AST related queries
GetPointerByteSize() const271 size_t CompilerType::GetPointerByteSize() const {
272   if (m_type_system)
273     return m_type_system->GetPointerByteSize();
274   return 0;
275 }
276 
GetTypeName() const277 ConstString CompilerType::GetTypeName() const {
278   if (IsValid()) {
279     return m_type_system->GetTypeName(m_type);
280   }
281   return ConstString("<invalid>");
282 }
283 
GetDisplayTypeName() const284 ConstString CompilerType::GetDisplayTypeName() const {
285   if (IsValid())
286     return m_type_system->GetDisplayTypeName(m_type);
287   return ConstString("<invalid>");
288 }
289 
GetTypeInfo(CompilerType * pointee_or_element_compiler_type) const290 uint32_t CompilerType::GetTypeInfo(
291     CompilerType *pointee_or_element_compiler_type) const {
292   if (!IsValid())
293     return 0;
294 
295   return m_type_system->GetTypeInfo(m_type, pointee_or_element_compiler_type);
296 }
297 
GetMinimumLanguage()298 lldb::LanguageType CompilerType::GetMinimumLanguage() {
299   if (!IsValid())
300     return lldb::eLanguageTypeC;
301 
302   return m_type_system->GetMinimumLanguage(m_type);
303 }
304 
GetTypeClass() const305 lldb::TypeClass CompilerType::GetTypeClass() const {
306   if (!IsValid())
307     return lldb::eTypeClassInvalid;
308 
309   return m_type_system->GetTypeClass(m_type);
310 }
311 
SetCompilerType(TypeSystem * type_system,lldb::opaque_compiler_type_t type)312 void CompilerType::SetCompilerType(TypeSystem *type_system,
313                                    lldb::opaque_compiler_type_t type) {
314   m_type_system = type_system;
315   m_type = type;
316 }
317 
GetTypeQualifiers() const318 unsigned CompilerType::GetTypeQualifiers() const {
319   if (IsValid())
320     return m_type_system->GetTypeQualifiers(m_type);
321   return 0;
322 }
323 
324 // Creating related types
325 
326 CompilerType
GetArrayElementType(ExecutionContextScope * exe_scope) const327 CompilerType::GetArrayElementType(ExecutionContextScope *exe_scope) const {
328   if (IsValid()) {
329     return m_type_system->GetArrayElementType(m_type, exe_scope);
330   }
331   return CompilerType();
332 }
333 
GetArrayType(uint64_t size) const334 CompilerType CompilerType::GetArrayType(uint64_t size) const {
335   if (IsValid()) {
336     return m_type_system->GetArrayType(m_type, size);
337   }
338   return CompilerType();
339 }
340 
GetCanonicalType() const341 CompilerType CompilerType::GetCanonicalType() const {
342   if (IsValid())
343     return m_type_system->GetCanonicalType(m_type);
344   return CompilerType();
345 }
346 
GetFullyUnqualifiedType() const347 CompilerType CompilerType::GetFullyUnqualifiedType() const {
348   if (IsValid())
349     return m_type_system->GetFullyUnqualifiedType(m_type);
350   return CompilerType();
351 }
352 
GetEnumerationIntegerType() const353 CompilerType CompilerType::GetEnumerationIntegerType() const {
354   if (IsValid())
355     return m_type_system->GetEnumerationIntegerType(m_type);
356   return CompilerType();
357 }
358 
GetFunctionArgumentCount() const359 int CompilerType::GetFunctionArgumentCount() const {
360   if (IsValid()) {
361     return m_type_system->GetFunctionArgumentCount(m_type);
362   }
363   return -1;
364 }
365 
GetFunctionArgumentTypeAtIndex(size_t idx) const366 CompilerType CompilerType::GetFunctionArgumentTypeAtIndex(size_t idx) const {
367   if (IsValid()) {
368     return m_type_system->GetFunctionArgumentTypeAtIndex(m_type, idx);
369   }
370   return CompilerType();
371 }
372 
GetFunctionReturnType() const373 CompilerType CompilerType::GetFunctionReturnType() const {
374   if (IsValid()) {
375     return m_type_system->GetFunctionReturnType(m_type);
376   }
377   return CompilerType();
378 }
379 
GetNumMemberFunctions() const380 size_t CompilerType::GetNumMemberFunctions() const {
381   if (IsValid()) {
382     return m_type_system->GetNumMemberFunctions(m_type);
383   }
384   return 0;
385 }
386 
GetMemberFunctionAtIndex(size_t idx)387 TypeMemberFunctionImpl CompilerType::GetMemberFunctionAtIndex(size_t idx) {
388   if (IsValid()) {
389     return m_type_system->GetMemberFunctionAtIndex(m_type, idx);
390   }
391   return TypeMemberFunctionImpl();
392 }
393 
GetNonReferenceType() const394 CompilerType CompilerType::GetNonReferenceType() const {
395   if (IsValid())
396     return m_type_system->GetNonReferenceType(m_type);
397   return CompilerType();
398 }
399 
GetPointeeType() const400 CompilerType CompilerType::GetPointeeType() const {
401   if (IsValid()) {
402     return m_type_system->GetPointeeType(m_type);
403   }
404   return CompilerType();
405 }
406 
GetPointerType() const407 CompilerType CompilerType::GetPointerType() const {
408   if (IsValid()) {
409     return m_type_system->GetPointerType(m_type);
410   }
411   return CompilerType();
412 }
413 
GetLValueReferenceType() const414 CompilerType CompilerType::GetLValueReferenceType() const {
415   if (IsValid())
416     return m_type_system->GetLValueReferenceType(m_type);
417   else
418     return CompilerType();
419 }
420 
GetRValueReferenceType() const421 CompilerType CompilerType::GetRValueReferenceType() const {
422   if (IsValid())
423     return m_type_system->GetRValueReferenceType(m_type);
424   else
425     return CompilerType();
426 }
427 
GetAtomicType() const428 CompilerType CompilerType::GetAtomicType() const {
429   if (IsValid())
430     return m_type_system->GetAtomicType(m_type);
431   return CompilerType();
432 }
433 
AddConstModifier() const434 CompilerType CompilerType::AddConstModifier() const {
435   if (IsValid())
436     return m_type_system->AddConstModifier(m_type);
437   else
438     return CompilerType();
439 }
440 
AddVolatileModifier() const441 CompilerType CompilerType::AddVolatileModifier() const {
442   if (IsValid())
443     return m_type_system->AddVolatileModifier(m_type);
444   else
445     return CompilerType();
446 }
447 
AddRestrictModifier() const448 CompilerType CompilerType::AddRestrictModifier() const {
449   if (IsValid())
450     return m_type_system->AddRestrictModifier(m_type);
451   else
452     return CompilerType();
453 }
454 
CreateTypedef(const char * name,const CompilerDeclContext & decl_ctx,uint32_t payload) const455 CompilerType CompilerType::CreateTypedef(const char *name,
456                                          const CompilerDeclContext &decl_ctx,
457                                          uint32_t payload) const {
458   if (IsValid())
459     return m_type_system->CreateTypedef(m_type, name, decl_ctx, payload);
460   else
461     return CompilerType();
462 }
463 
GetTypedefedType() const464 CompilerType CompilerType::GetTypedefedType() const {
465   if (IsValid())
466     return m_type_system->GetTypedefedType(m_type);
467   else
468     return CompilerType();
469 }
470 
471 // Create related types using the current type's AST
472 
473 CompilerType
GetBasicTypeFromAST(lldb::BasicType basic_type) const474 CompilerType::GetBasicTypeFromAST(lldb::BasicType basic_type) const {
475   if (IsValid())
476     return m_type_system->GetBasicTypeFromAST(basic_type);
477   return CompilerType();
478 }
479 // Exploring the type
480 
481 llvm::Optional<uint64_t>
GetBitSize(ExecutionContextScope * exe_scope) const482 CompilerType::GetBitSize(ExecutionContextScope *exe_scope) const {
483   if (IsValid())
484     return m_type_system->GetBitSize(m_type, exe_scope);
485   return {};
486 }
487 
488 llvm::Optional<uint64_t>
GetByteSize(ExecutionContextScope * exe_scope) const489 CompilerType::GetByteSize(ExecutionContextScope *exe_scope) const {
490   if (llvm::Optional<uint64_t> bit_size = GetBitSize(exe_scope))
491     return (*bit_size + 7) / 8;
492   return {};
493 }
494 
GetTypeBitAlign(ExecutionContextScope * exe_scope) const495 llvm::Optional<size_t> CompilerType::GetTypeBitAlign(ExecutionContextScope *exe_scope) const {
496   if (IsValid())
497     return m_type_system->GetTypeBitAlign(m_type, exe_scope);
498   return {};
499 }
500 
GetEncoding(uint64_t & count) const501 lldb::Encoding CompilerType::GetEncoding(uint64_t &count) const {
502   if (!IsValid())
503     return lldb::eEncodingInvalid;
504 
505   return m_type_system->GetEncoding(m_type, count);
506 }
507 
GetFormat() const508 lldb::Format CompilerType::GetFormat() const {
509   if (!IsValid())
510     return lldb::eFormatDefault;
511 
512   return m_type_system->GetFormat(m_type);
513 }
514 
GetNumChildren(bool omit_empty_base_classes,const ExecutionContext * exe_ctx) const515 uint32_t CompilerType::GetNumChildren(bool omit_empty_base_classes,
516                                       const ExecutionContext *exe_ctx) const {
517   if (!IsValid())
518     return 0;
519   return m_type_system->GetNumChildren(m_type, omit_empty_base_classes,
520                                        exe_ctx);
521 }
522 
GetBasicTypeEnumeration() const523 lldb::BasicType CompilerType::GetBasicTypeEnumeration() const {
524   if (IsValid())
525     return m_type_system->GetBasicTypeEnumeration(m_type);
526   return eBasicTypeInvalid;
527 }
528 
ForEachEnumerator(std::function<bool (const CompilerType & integer_type,ConstString name,const llvm::APSInt & value)> const & callback) const529 void CompilerType::ForEachEnumerator(
530     std::function<bool(const CompilerType &integer_type,
531                        ConstString name,
532                        const llvm::APSInt &value)> const &callback) const {
533   if (IsValid())
534     return m_type_system->ForEachEnumerator(m_type, callback);
535 }
536 
GetNumFields() const537 uint32_t CompilerType::GetNumFields() const {
538   if (!IsValid())
539     return 0;
540   return m_type_system->GetNumFields(m_type);
541 }
542 
GetFieldAtIndex(size_t idx,std::string & name,uint64_t * bit_offset_ptr,uint32_t * bitfield_bit_size_ptr,bool * is_bitfield_ptr) const543 CompilerType CompilerType::GetFieldAtIndex(size_t idx, std::string &name,
544                                            uint64_t *bit_offset_ptr,
545                                            uint32_t *bitfield_bit_size_ptr,
546                                            bool *is_bitfield_ptr) const {
547   if (!IsValid())
548     return CompilerType();
549   return m_type_system->GetFieldAtIndex(m_type, idx, name, bit_offset_ptr,
550                                         bitfield_bit_size_ptr, is_bitfield_ptr);
551 }
552 
GetNumDirectBaseClasses() const553 uint32_t CompilerType::GetNumDirectBaseClasses() const {
554   if (IsValid())
555     return m_type_system->GetNumDirectBaseClasses(m_type);
556   return 0;
557 }
558 
GetNumVirtualBaseClasses() const559 uint32_t CompilerType::GetNumVirtualBaseClasses() const {
560   if (IsValid())
561     return m_type_system->GetNumVirtualBaseClasses(m_type);
562   return 0;
563 }
564 
565 CompilerType
GetDirectBaseClassAtIndex(size_t idx,uint32_t * bit_offset_ptr) const566 CompilerType::GetDirectBaseClassAtIndex(size_t idx,
567                                         uint32_t *bit_offset_ptr) const {
568   if (IsValid())
569     return m_type_system->GetDirectBaseClassAtIndex(m_type, idx,
570                                                     bit_offset_ptr);
571   return CompilerType();
572 }
573 
574 CompilerType
GetVirtualBaseClassAtIndex(size_t idx,uint32_t * bit_offset_ptr) const575 CompilerType::GetVirtualBaseClassAtIndex(size_t idx,
576                                          uint32_t *bit_offset_ptr) const {
577   if (IsValid())
578     return m_type_system->GetVirtualBaseClassAtIndex(m_type, idx,
579                                                      bit_offset_ptr);
580   return CompilerType();
581 }
582 
GetIndexOfFieldWithName(const char * name,CompilerType * field_compiler_type_ptr,uint64_t * bit_offset_ptr,uint32_t * bitfield_bit_size_ptr,bool * is_bitfield_ptr) const583 uint32_t CompilerType::GetIndexOfFieldWithName(
584     const char *name, CompilerType *field_compiler_type_ptr,
585     uint64_t *bit_offset_ptr, uint32_t *bitfield_bit_size_ptr,
586     bool *is_bitfield_ptr) const {
587   unsigned count = GetNumFields();
588   std::string field_name;
589   for (unsigned index = 0; index < count; index++) {
590     CompilerType field_compiler_type(
591         GetFieldAtIndex(index, field_name, bit_offset_ptr,
592                         bitfield_bit_size_ptr, is_bitfield_ptr));
593     if (strcmp(field_name.c_str(), name) == 0) {
594       if (field_compiler_type_ptr)
595         *field_compiler_type_ptr = field_compiler_type;
596       return index;
597     }
598   }
599   return UINT32_MAX;
600 }
601 
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) const602 CompilerType CompilerType::GetChildCompilerTypeAtIndex(
603     ExecutionContext *exe_ctx, size_t idx, bool transparent_pointers,
604     bool omit_empty_base_classes, bool ignore_array_bounds,
605     std::string &child_name, uint32_t &child_byte_size,
606     int32_t &child_byte_offset, uint32_t &child_bitfield_bit_size,
607     uint32_t &child_bitfield_bit_offset, bool &child_is_base_class,
608     bool &child_is_deref_of_parent, ValueObject *valobj,
609     uint64_t &language_flags) const {
610   if (!IsValid())
611     return CompilerType();
612   return m_type_system->GetChildCompilerTypeAtIndex(
613       m_type, exe_ctx, idx, transparent_pointers, omit_empty_base_classes,
614       ignore_array_bounds, child_name, child_byte_size, child_byte_offset,
615       child_bitfield_bit_size, child_bitfield_bit_offset, child_is_base_class,
616       child_is_deref_of_parent, valobj, language_flags);
617 }
618 
619 // Look for a child member (doesn't include base classes, but it does include
620 // their members) in the type hierarchy. Returns an index path into
621 // "clang_type" on how to reach the appropriate member.
622 //
623 //    class A
624 //    {
625 //    public:
626 //        int m_a;
627 //        int m_b;
628 //    };
629 //
630 //    class B
631 //    {
632 //    };
633 //
634 //    class C :
635 //        public B,
636 //        public A
637 //    {
638 //    };
639 //
640 // If we have a clang type that describes "class C", and we wanted to looked
641 // "m_b" in it:
642 //
643 // With omit_empty_base_classes == false we would get an integer array back
644 // with: { 1,  1 } The first index 1 is the child index for "class A" within
645 // class C The second index 1 is the child index for "m_b" within class A
646 //
647 // With omit_empty_base_classes == true we would get an integer array back
648 // with: { 0,  1 } The first index 0 is the child index for "class A" within
649 // class C (since class B doesn't have any members it doesn't count) The second
650 // index 1 is the child index for "m_b" within class A
651 
GetIndexOfChildMemberWithName(const char * name,bool omit_empty_base_classes,std::vector<uint32_t> & child_indexes) const652 size_t CompilerType::GetIndexOfChildMemberWithName(
653     const char *name, bool omit_empty_base_classes,
654     std::vector<uint32_t> &child_indexes) const {
655   if (IsValid() && name && name[0]) {
656     return m_type_system->GetIndexOfChildMemberWithName(
657         m_type, name, omit_empty_base_classes, child_indexes);
658   }
659   return 0;
660 }
661 
GetNumTemplateArguments() const662 size_t CompilerType::GetNumTemplateArguments() const {
663   if (IsValid()) {
664     return m_type_system->GetNumTemplateArguments(m_type);
665   }
666   return 0;
667 }
668 
GetTemplateArgumentKind(size_t idx) const669 TemplateArgumentKind CompilerType::GetTemplateArgumentKind(size_t idx) const {
670   if (IsValid())
671     return m_type_system->GetTemplateArgumentKind(m_type, idx);
672   return eTemplateArgumentKindNull;
673 }
674 
GetTypeTemplateArgument(size_t idx) const675 CompilerType CompilerType::GetTypeTemplateArgument(size_t idx) const {
676   if (IsValid()) {
677     return m_type_system->GetTypeTemplateArgument(m_type, idx);
678   }
679   return CompilerType();
680 }
681 
682 llvm::Optional<CompilerType::IntegralTemplateArgument>
GetIntegralTemplateArgument(size_t idx) const683 CompilerType::GetIntegralTemplateArgument(size_t idx) const {
684   if (IsValid())
685     return m_type_system->GetIntegralTemplateArgument(m_type, idx);
686   return llvm::None;
687 }
688 
GetTypeForFormatters() const689 CompilerType CompilerType::GetTypeForFormatters() const {
690   if (IsValid())
691     return m_type_system->GetTypeForFormatters(m_type);
692   return CompilerType();
693 }
694 
ShouldPrintAsOneLiner(ValueObject * valobj) const695 LazyBool CompilerType::ShouldPrintAsOneLiner(ValueObject *valobj) const {
696   if (IsValid())
697     return m_type_system->ShouldPrintAsOneLiner(m_type, valobj);
698   return eLazyBoolCalculate;
699 }
700 
IsMeaninglessWithoutDynamicResolution() const701 bool CompilerType::IsMeaninglessWithoutDynamicResolution() const {
702   if (IsValid())
703     return m_type_system->IsMeaninglessWithoutDynamicResolution(m_type);
704   return false;
705 }
706 
707 // Get the index of the child of "clang_type" whose name matches. This function
708 // doesn't descend into the children, but only looks one level deep and name
709 // matches can include base class names.
710 
711 uint32_t
GetIndexOfChildWithName(const char * name,bool omit_empty_base_classes) const712 CompilerType::GetIndexOfChildWithName(const char *name,
713                                       bool omit_empty_base_classes) const {
714   if (IsValid() && name && name[0]) {
715     return m_type_system->GetIndexOfChildWithName(m_type, name,
716                                                   omit_empty_base_classes);
717   }
718   return UINT32_MAX;
719 }
720 
721 // Dumping types
722 
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)723 void CompilerType::DumpValue(ExecutionContext *exe_ctx, Stream *s,
724                              lldb::Format format, const DataExtractor &data,
725                              lldb::offset_t data_byte_offset,
726                              size_t data_byte_size, uint32_t bitfield_bit_size,
727                              uint32_t bitfield_bit_offset, bool show_types,
728                              bool show_summary, bool verbose, uint32_t depth) {
729   if (!IsValid())
730     return;
731   m_type_system->DumpValue(m_type, exe_ctx, s, format, data, data_byte_offset,
732                            data_byte_size, bitfield_bit_size,
733                            bitfield_bit_offset, show_types, show_summary,
734                            verbose, depth);
735 }
736 
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)737 bool CompilerType::DumpTypeValue(Stream *s, lldb::Format format,
738                                  const DataExtractor &data,
739                                  lldb::offset_t byte_offset, size_t byte_size,
740                                  uint32_t bitfield_bit_size,
741                                  uint32_t bitfield_bit_offset,
742                                  ExecutionContextScope *exe_scope) {
743   if (!IsValid())
744     return false;
745   return m_type_system->DumpTypeValue(m_type, s, format, data, byte_offset,
746                                       byte_size, bitfield_bit_size,
747                                       bitfield_bit_offset, exe_scope);
748 }
749 
DumpSummary(ExecutionContext * exe_ctx,Stream * s,const DataExtractor & data,lldb::offset_t data_byte_offset,size_t data_byte_size)750 void CompilerType::DumpSummary(ExecutionContext *exe_ctx, Stream *s,
751                                const DataExtractor &data,
752                                lldb::offset_t data_byte_offset,
753                                size_t data_byte_size) {
754   if (IsValid())
755     m_type_system->DumpSummary(m_type, exe_ctx, s, data, data_byte_offset,
756                                data_byte_size);
757 }
758 
DumpTypeDescription(lldb::DescriptionLevel level) const759 void CompilerType::DumpTypeDescription(lldb::DescriptionLevel level) const {
760   if (IsValid())
761     m_type_system->DumpTypeDescription(m_type, level);
762 }
763 
DumpTypeDescription(Stream * s,lldb::DescriptionLevel level) const764 void CompilerType::DumpTypeDescription(Stream *s,
765                                        lldb::DescriptionLevel level) const {
766   if (IsValid()) {
767     m_type_system->DumpTypeDescription(m_type, s, level);
768   }
769 }
770 
771 #ifndef NDEBUG
dump() const772 LLVM_DUMP_METHOD void CompilerType::dump() const {
773   if (IsValid())
774     m_type_system->dump(m_type);
775   else
776     llvm::errs() << "<invalid>\n";
777 }
778 #endif
779 
GetValueAsScalar(const lldb_private::DataExtractor & data,lldb::offset_t data_byte_offset,size_t data_byte_size,Scalar & value,ExecutionContextScope * exe_scope) const780 bool CompilerType::GetValueAsScalar(const lldb_private::DataExtractor &data,
781                                     lldb::offset_t data_byte_offset,
782                                     size_t data_byte_size, Scalar &value,
783                                     ExecutionContextScope *exe_scope) const {
784   if (!IsValid())
785     return false;
786 
787   if (IsAggregateType()) {
788     return false; // Aggregate types don't have scalar values
789   } else {
790     uint64_t count = 0;
791     lldb::Encoding encoding = GetEncoding(count);
792 
793     if (encoding == lldb::eEncodingInvalid || count != 1)
794       return false;
795 
796     llvm::Optional<uint64_t> byte_size = GetByteSize(exe_scope);
797     if (!byte_size)
798       return false;
799     lldb::offset_t offset = data_byte_offset;
800     switch (encoding) {
801     case lldb::eEncodingInvalid:
802       break;
803     case lldb::eEncodingVector:
804       break;
805     case lldb::eEncodingUint:
806       if (*byte_size <= sizeof(unsigned long long)) {
807         uint64_t uval64 = data.GetMaxU64(&offset, *byte_size);
808         if (*byte_size <= sizeof(unsigned int)) {
809           value = (unsigned int)uval64;
810           return true;
811         } else if (*byte_size <= sizeof(unsigned long)) {
812           value = (unsigned long)uval64;
813           return true;
814         } else if (*byte_size <= sizeof(unsigned long long)) {
815           value = (unsigned long long)uval64;
816           return true;
817         } else
818           value.Clear();
819       }
820       break;
821 
822     case lldb::eEncodingSint:
823       if (*byte_size <= sizeof(long long)) {
824         int64_t sval64 = data.GetMaxS64(&offset, *byte_size);
825         if (*byte_size <= sizeof(int)) {
826           value = (int)sval64;
827           return true;
828         } else if (*byte_size <= sizeof(long)) {
829           value = (long)sval64;
830           return true;
831         } else if (*byte_size <= sizeof(long long)) {
832           value = (long long)sval64;
833           return true;
834         } else
835           value.Clear();
836       }
837       break;
838 
839     case lldb::eEncodingIEEE754:
840       if (*byte_size <= sizeof(long double)) {
841         uint32_t u32;
842         uint64_t u64;
843         if (*byte_size == sizeof(float)) {
844           if (sizeof(float) == sizeof(uint32_t)) {
845             u32 = data.GetU32(&offset);
846             value = *((float *)&u32);
847             return true;
848           } else if (sizeof(float) == sizeof(uint64_t)) {
849             u64 = data.GetU64(&offset);
850             value = *((float *)&u64);
851             return true;
852           }
853         } else if (*byte_size == sizeof(double)) {
854           if (sizeof(double) == sizeof(uint32_t)) {
855             u32 = data.GetU32(&offset);
856             value = *((double *)&u32);
857             return true;
858           } else if (sizeof(double) == sizeof(uint64_t)) {
859             u64 = data.GetU64(&offset);
860             value = *((double *)&u64);
861             return true;
862           }
863         } else if (*byte_size == sizeof(long double)) {
864           if (sizeof(long double) == sizeof(uint32_t)) {
865             u32 = data.GetU32(&offset);
866             value = *((long double *)&u32);
867             return true;
868           } else if (sizeof(long double) == sizeof(uint64_t)) {
869             u64 = data.GetU64(&offset);
870             value = *((long double *)&u64);
871             return true;
872           }
873         }
874       }
875       break;
876     }
877   }
878   return false;
879 }
880 
881 #ifndef NDEBUG
Verify() const882 bool CompilerType::Verify() const {
883   return !IsValid() || m_type_system->Verify(m_type);
884 }
885 #endif
886 
operator ==(const lldb_private::CompilerType & lhs,const lldb_private::CompilerType & rhs)887 bool lldb_private::operator==(const lldb_private::CompilerType &lhs,
888                               const lldb_private::CompilerType &rhs) {
889   return lhs.GetTypeSystem() == rhs.GetTypeSystem() &&
890          lhs.GetOpaqueQualType() == rhs.GetOpaqueQualType();
891 }
892 
operator !=(const lldb_private::CompilerType & lhs,const lldb_private::CompilerType & rhs)893 bool lldb_private::operator!=(const lldb_private::CompilerType &lhs,
894                               const lldb_private::CompilerType &rhs) {
895   return !(lhs == rhs);
896 }
897