1 //===-- CompilerType.cpp --------------------------------------------------===//
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
7 //===----------------------------------------------------------------------===//
9 #include "lldb/Symbol/CompilerType.h"
11 #include "lldb/Core/Debugger.h"
12 #include "lldb/Symbol/Type.h"
13 #include "lldb/Target/ExecutionContext.h"
14 #include "lldb/Target/Process.h"
15 #include "lldb/Utility/ConstString.h"
16 #include "lldb/Utility/DataBufferHeap.h"
17 #include "lldb/Utility/DataExtractor.h"
18 #include "lldb/Utility/Scalar.h"
19 #include "lldb/Utility/Stream.h"
20 #include "lldb/Utility/StreamString.h"
27 using namespace lldb_private
;
31 bool CompilerType::IsAggregateType() const {
33 if (auto type_system_sp
= GetTypeSystem())
34 return type_system_sp
->IsAggregateType(m_type
);
38 bool CompilerType::IsAnonymousType() const {
40 if (auto type_system_sp
= GetTypeSystem())
41 return type_system_sp
->IsAnonymousType(m_type
);
45 bool CompilerType::IsScopedEnumerationType() const {
47 if (auto type_system_sp
= GetTypeSystem())
48 return type_system_sp
->IsScopedEnumerationType(m_type
);
52 bool CompilerType::IsArrayType(CompilerType
*element_type_ptr
, uint64_t *size
,
53 bool *is_incomplete
) const {
55 if (auto type_system_sp
= GetTypeSystem())
56 return type_system_sp
->IsArrayType(m_type
, element_type_ptr
, size
,
60 element_type_ptr
->Clear();
64 *is_incomplete
= false;
68 bool CompilerType::IsVectorType(CompilerType
*element_type
,
69 uint64_t *size
) const {
71 if (auto type_system_sp
= GetTypeSystem())
72 return type_system_sp
->IsVectorType(m_type
, element_type
, size
);
76 bool CompilerType::IsRuntimeGeneratedType() const {
78 if (auto type_system_sp
= GetTypeSystem())
79 return type_system_sp
->IsRuntimeGeneratedType(m_type
);
83 bool CompilerType::IsCharType() const {
85 if (auto type_system_sp
= GetTypeSystem())
86 return type_system_sp
->IsCharType(m_type
);
90 bool CompilerType::IsCompleteType() const {
92 if (auto type_system_sp
= GetTypeSystem())
93 return type_system_sp
->IsCompleteType(m_type
);
97 bool CompilerType::IsForcefullyCompleted() const {
99 if (auto type_system_sp
= GetTypeSystem())
100 return type_system_sp
->IsForcefullyCompleted(m_type
);
104 bool CompilerType::IsConst() const {
106 if (auto type_system_sp
= GetTypeSystem())
107 return type_system_sp
->IsConst(m_type
);
111 unsigned CompilerType::GetPtrAuthKey() const {
113 if (auto type_system_sp
= GetTypeSystem())
114 return type_system_sp
->GetPtrAuthKey(m_type
);
118 unsigned CompilerType::GetPtrAuthDiscriminator() const {
120 if (auto type_system_sp
= GetTypeSystem())
121 return type_system_sp
->GetPtrAuthDiscriminator(m_type
);
125 bool CompilerType::GetPtrAuthAddressDiversity() const {
127 if (auto type_system_sp
= GetTypeSystem())
128 return type_system_sp
->GetPtrAuthAddressDiversity(m_type
);
132 bool CompilerType::IsFunctionType() const {
134 if (auto type_system_sp
= GetTypeSystem())
135 return type_system_sp
->IsFunctionType(m_type
);
139 // Used to detect "Homogeneous Floating-point Aggregates"
141 CompilerType::IsHomogeneousAggregate(CompilerType
*base_type_ptr
) const {
143 if (auto type_system_sp
= GetTypeSystem())
144 return type_system_sp
->IsHomogeneousAggregate(m_type
, base_type_ptr
);
148 size_t CompilerType::GetNumberOfFunctionArguments() const {
150 if (auto type_system_sp
= GetTypeSystem())
151 return type_system_sp
->GetNumberOfFunctionArguments(m_type
);
156 CompilerType::GetFunctionArgumentAtIndex(const size_t index
) const {
158 if (auto type_system_sp
= GetTypeSystem())
159 return type_system_sp
->GetFunctionArgumentAtIndex(m_type
, index
);
160 return CompilerType();
163 bool CompilerType::IsFunctionPointerType() const {
165 if (auto type_system_sp
= GetTypeSystem())
166 return type_system_sp
->IsFunctionPointerType(m_type
);
170 bool CompilerType::IsMemberFunctionPointerType() const {
172 if (auto type_system_sp
= GetTypeSystem())
173 return type_system_sp
->IsMemberFunctionPointerType(m_type
);
177 bool CompilerType::IsBlockPointerType(
178 CompilerType
*function_pointer_type_ptr
) const {
180 if (auto type_system_sp
= GetTypeSystem())
181 return type_system_sp
->IsBlockPointerType(m_type
, function_pointer_type_ptr
);
185 bool CompilerType::IsIntegerType(bool &is_signed
) const {
187 if (auto type_system_sp
= GetTypeSystem())
188 return type_system_sp
->IsIntegerType(m_type
, is_signed
);
192 bool CompilerType::IsEnumerationType(bool &is_signed
) const {
194 if (auto type_system_sp
= GetTypeSystem())
195 return type_system_sp
->IsEnumerationType(m_type
, is_signed
);
199 bool CompilerType::IsIntegerOrEnumerationType(bool &is_signed
) const {
200 return IsIntegerType(is_signed
) || IsEnumerationType(is_signed
);
203 bool CompilerType::IsPointerType(CompilerType
*pointee_type
) const {
205 if (auto type_system_sp
= GetTypeSystem())
206 return type_system_sp
->IsPointerType(m_type
, pointee_type
);
209 pointee_type
->Clear();
213 bool CompilerType::IsPointerOrReferenceType(CompilerType
*pointee_type
) const {
215 if (auto type_system_sp
= GetTypeSystem())
216 return type_system_sp
->IsPointerOrReferenceType(m_type
, pointee_type
);
219 pointee_type
->Clear();
223 bool CompilerType::IsReferenceType(CompilerType
*pointee_type
,
224 bool *is_rvalue
) const {
226 if (auto type_system_sp
= GetTypeSystem())
227 return type_system_sp
->IsReferenceType(m_type
, pointee_type
, is_rvalue
);
230 pointee_type
->Clear();
234 bool CompilerType::ShouldTreatScalarValueAsAddress() const {
236 if (auto type_system_sp
= GetTypeSystem())
237 return type_system_sp
->ShouldTreatScalarValueAsAddress(m_type
);
241 bool CompilerType::IsFloatingPointType(uint32_t &count
,
242 bool &is_complex
) const {
244 if (auto type_system_sp
= GetTypeSystem())
245 return type_system_sp
->IsFloatingPointType(m_type
, count
, is_complex
);
252 bool CompilerType::IsDefined() const {
254 if (auto type_system_sp
= GetTypeSystem())
255 return type_system_sp
->IsDefined(m_type
);
259 bool CompilerType::IsPolymorphicClass() const {
261 if (auto type_system_sp
= GetTypeSystem())
262 return type_system_sp
->IsPolymorphicClass(m_type
);
267 bool CompilerType::IsPossibleDynamicType(CompilerType
*dynamic_pointee_type
,
268 bool check_cplusplus
,
269 bool check_objc
) const {
271 if (auto type_system_sp
= GetTypeSystem())
272 return type_system_sp
->IsPossibleDynamicType(m_type
, dynamic_pointee_type
,
273 check_cplusplus
, check_objc
);
277 bool CompilerType::IsScalarType() const {
279 if (auto type_system_sp
= GetTypeSystem())
280 return type_system_sp
->IsScalarType(m_type
);
284 bool CompilerType::IsTemplateType() const {
286 if (auto type_system_sp
= GetTypeSystem())
287 return type_system_sp
->IsTemplateType(m_type
);
291 bool CompilerType::IsTypedefType() const {
293 if (auto type_system_sp
= GetTypeSystem())
294 return type_system_sp
->IsTypedefType(m_type
);
298 bool CompilerType::IsVoidType() const {
300 if (auto type_system_sp
= GetTypeSystem())
301 return type_system_sp
->IsVoidType(m_type
);
305 bool CompilerType::IsPointerToScalarType() const {
309 return IsPointerType() && GetPointeeType().IsScalarType();
312 bool CompilerType::IsArrayOfScalarType() const {
313 CompilerType element_type
;
314 if (IsArrayType(&element_type
))
315 return element_type
.IsScalarType();
319 bool CompilerType::IsBeingDefined() const {
321 if (auto type_system_sp
= GetTypeSystem())
322 return type_system_sp
->IsBeingDefined(m_type
);
326 bool CompilerType::IsInteger() const {
327 bool is_signed
= false; // May be reset by the call below.
328 return IsIntegerType(is_signed
);
331 bool CompilerType::IsFloat() const {
333 bool is_complex
= false;
334 return IsFloatingPointType(count
, is_complex
);
337 bool CompilerType::IsEnumerationType() const {
338 bool is_signed
= false; // May be reset by the call below.
339 return IsEnumerationType(is_signed
);
342 bool CompilerType::IsUnscopedEnumerationType() const {
343 return IsEnumerationType() && !IsScopedEnumerationType();
346 bool CompilerType::IsIntegerOrUnscopedEnumerationType() const {
347 return IsInteger() || IsUnscopedEnumerationType();
350 bool CompilerType::IsSigned() const {
351 return GetTypeInfo() & lldb::eTypeIsSigned
;
354 bool CompilerType::IsNullPtrType() const {
355 return GetCanonicalType().GetBasicTypeEnumeration() ==
356 lldb::eBasicTypeNullPtr
;
359 bool CompilerType::IsBoolean() const {
360 return GetCanonicalType().GetBasicTypeEnumeration() == lldb::eBasicTypeBool
;
363 bool CompilerType::IsEnumerationIntegerTypeSigned() const {
365 return GetEnumerationIntegerType().GetTypeInfo() & lldb::eTypeIsSigned
;
370 bool CompilerType::IsScalarOrUnscopedEnumerationType() const {
371 return IsScalarType() || IsUnscopedEnumerationType();
374 bool CompilerType::IsPromotableIntegerType() const {
375 // Unscoped enums are always considered as promotable, even if their
376 // underlying type does not need to be promoted (e.g. "int").
377 if (IsUnscopedEnumerationType())
380 switch (GetCanonicalType().GetBasicTypeEnumeration()) {
381 case lldb::eBasicTypeBool
:
382 case lldb::eBasicTypeChar
:
383 case lldb::eBasicTypeSignedChar
:
384 case lldb::eBasicTypeUnsignedChar
:
385 case lldb::eBasicTypeShort
:
386 case lldb::eBasicTypeUnsignedShort
:
387 case lldb::eBasicTypeWChar
:
388 case lldb::eBasicTypeSignedWChar
:
389 case lldb::eBasicTypeUnsignedWChar
:
390 case lldb::eBasicTypeChar16
:
391 case lldb::eBasicTypeChar32
:
398 llvm_unreachable("All cases handled above.");
401 bool CompilerType::IsPointerToVoid() const {
405 return IsPointerType() &&
406 GetPointeeType().GetBasicTypeEnumeration() == lldb::eBasicTypeVoid
;
409 bool CompilerType::IsRecordType() const {
413 return GetCanonicalType().GetTypeClass() &
414 (lldb::eTypeClassClass
| lldb::eTypeClassStruct
|
415 lldb::eTypeClassUnion
);
418 bool CompilerType::IsVirtualBase(CompilerType target_base
,
419 CompilerType
*virtual_base
,
420 bool carry_virtual
) const {
421 if (CompareTypes(target_base
))
422 return carry_virtual
;
424 if (!carry_virtual
) {
425 uint32_t num_virtual_bases
= GetNumVirtualBaseClasses();
426 for (uint32_t i
= 0; i
< num_virtual_bases
; ++i
) {
428 auto base
= GetVirtualBaseClassAtIndex(i
, &bit_offset
);
429 if (base
.IsVirtualBase(target_base
, virtual_base
,
430 /*carry_virtual*/ true)) {
432 *virtual_base
= base
;
439 uint32_t num_direct_bases
= GetNumDirectBaseClasses();
440 for (uint32_t i
= 0; i
< num_direct_bases
; ++i
) {
442 auto base
= GetDirectBaseClassAtIndex(i
, &bit_offset
);
443 if (base
.IsVirtualBase(target_base
, virtual_base
, carry_virtual
))
450 bool CompilerType::IsContextuallyConvertibleToBool() const {
451 return IsScalarType() || IsUnscopedEnumerationType() || IsPointerType() ||
452 IsNullPtrType() || IsArrayType();
455 bool CompilerType::IsBasicType() const {
456 return GetCanonicalType().GetBasicTypeEnumeration() !=
457 lldb::eBasicTypeInvalid
;
460 std::string
CompilerType::TypeDescription() {
461 auto name
= GetTypeName();
462 auto canonical_name
= GetCanonicalType().GetTypeName();
463 if (name
.IsEmpty() || canonical_name
.IsEmpty())
464 return "''"; // Should not happen, unless the input is broken somehow.
466 if (name
== canonical_name
)
467 return llvm::formatv("'{0}'", name
);
469 return llvm::formatv("'{0}' (canonically referred to as '{1}')", name
,
473 bool CompilerType::CompareTypes(CompilerType rhs
) const {
477 const ConstString name
= GetFullyUnqualifiedType().GetTypeName();
478 const ConstString rhs_name
= rhs
.GetFullyUnqualifiedType().GetTypeName();
479 return name
== rhs_name
;
482 const char *CompilerType::GetTypeTag() {
483 switch (GetTypeClass()) {
484 case lldb::eTypeClassClass
:
486 case lldb::eTypeClassEnumeration
:
488 case lldb::eTypeClassStruct
:
490 case lldb::eTypeClassUnion
:
495 llvm_unreachable("All cases are covered by code above.");
498 uint32_t CompilerType::GetNumberOfNonEmptyBaseClasses() {
500 uint32_t num_direct_bases
= GetNumDirectBaseClasses();
502 for (uint32_t i
= 0; i
< num_direct_bases
; ++i
) {
504 CompilerType base_type
= GetDirectBaseClassAtIndex(i
, &bit_offset
);
505 if (base_type
.GetNumFields() > 0 ||
506 base_type
.GetNumberOfNonEmptyBaseClasses() > 0)
514 bool CompilerType::GetCompleteType() const {
516 if (auto type_system_sp
= GetTypeSystem())
517 return type_system_sp
->GetCompleteType(m_type
);
521 // AST related queries
522 size_t CompilerType::GetPointerByteSize() const {
523 if (auto type_system_sp
= GetTypeSystem())
524 return type_system_sp
->GetPointerByteSize();
528 ConstString
CompilerType::GetTypeName(bool BaseOnly
) const {
530 if (auto type_system_sp
= GetTypeSystem())
531 return type_system_sp
->GetTypeName(m_type
, BaseOnly
);
533 return ConstString("<invalid>");
536 ConstString
CompilerType::GetDisplayTypeName() const {
538 if (auto type_system_sp
= GetTypeSystem())
539 return type_system_sp
->GetDisplayTypeName(m_type
);
540 return ConstString("<invalid>");
543 ConstString
CompilerType::GetMangledTypeName() const {
545 if (auto type_system_sp
= GetTypeSystem())
546 return type_system_sp
->GetMangledTypeName(m_type
);
548 return ConstString("<invalid>");
551 uint32_t CompilerType::GetTypeInfo(
552 CompilerType
*pointee_or_element_compiler_type
) const {
554 if (auto type_system_sp
= GetTypeSystem())
555 return type_system_sp
->GetTypeInfo(m_type
,
556 pointee_or_element_compiler_type
);
560 lldb::LanguageType
CompilerType::GetMinimumLanguage() {
562 if (auto type_system_sp
= GetTypeSystem())
563 return type_system_sp
->GetMinimumLanguage(m_type
);
564 return lldb::eLanguageTypeC
;
567 lldb::TypeClass
CompilerType::GetTypeClass() const {
569 if (auto type_system_sp
= GetTypeSystem())
570 return type_system_sp
->GetTypeClass(m_type
);
571 return lldb::eTypeClassInvalid
;
574 void CompilerType::SetCompilerType(lldb::TypeSystemWP type_system
,
575 lldb::opaque_compiler_type_t type
) {
576 m_type_system
= type_system
;
580 void CompilerType::SetCompilerType(CompilerType::TypeSystemSPWrapper type_system
,
581 lldb::opaque_compiler_type_t type
) {
582 m_type_system
= type_system
.GetSharedPointer();
586 unsigned CompilerType::GetTypeQualifiers() const {
588 if (auto type_system_sp
= GetTypeSystem())
589 return type_system_sp
->GetTypeQualifiers(m_type
);
593 // Creating related types
596 CompilerType::GetArrayElementType(ExecutionContextScope
*exe_scope
) const {
598 if (auto type_system_sp
= GetTypeSystem())
599 return type_system_sp
->GetArrayElementType(m_type
, exe_scope
);
601 return CompilerType();
604 CompilerType
CompilerType::GetArrayType(uint64_t size
) const {
606 if (auto type_system_sp
= GetTypeSystem())
607 return type_system_sp
->GetArrayType(m_type
, size
);
609 return CompilerType();
612 CompilerType
CompilerType::GetCanonicalType() const {
614 if (auto type_system_sp
= GetTypeSystem())
615 return type_system_sp
->GetCanonicalType(m_type
);
616 return CompilerType();
619 CompilerType
CompilerType::GetFullyUnqualifiedType() const {
621 if (auto type_system_sp
= GetTypeSystem())
622 return type_system_sp
->GetFullyUnqualifiedType(m_type
);
623 return CompilerType();
626 CompilerType
CompilerType::GetEnumerationIntegerType() const {
628 if (auto type_system_sp
= GetTypeSystem())
629 return type_system_sp
->GetEnumerationIntegerType(m_type
);
630 return CompilerType();
633 int CompilerType::GetFunctionArgumentCount() const {
635 if (auto type_system_sp
= GetTypeSystem())
636 return type_system_sp
->GetFunctionArgumentCount(m_type
);
641 CompilerType
CompilerType::GetFunctionArgumentTypeAtIndex(size_t idx
) const {
643 if (auto type_system_sp
= GetTypeSystem())
644 return type_system_sp
->GetFunctionArgumentTypeAtIndex(m_type
, idx
);
646 return CompilerType();
649 CompilerType
CompilerType::GetFunctionReturnType() const {
651 if (auto type_system_sp
= GetTypeSystem())
652 return type_system_sp
->GetFunctionReturnType(m_type
);
654 return CompilerType();
657 size_t CompilerType::GetNumMemberFunctions() const {
659 if (auto type_system_sp
= GetTypeSystem())
660 return type_system_sp
->GetNumMemberFunctions(m_type
);
665 TypeMemberFunctionImpl
CompilerType::GetMemberFunctionAtIndex(size_t idx
) {
667 if (auto type_system_sp
= GetTypeSystem())
668 return type_system_sp
->GetMemberFunctionAtIndex(m_type
, idx
);
670 return TypeMemberFunctionImpl();
673 CompilerType
CompilerType::GetNonReferenceType() const {
675 if (auto type_system_sp
= GetTypeSystem())
676 return type_system_sp
->GetNonReferenceType(m_type
);
677 return CompilerType();
680 CompilerType
CompilerType::GetPointeeType() const {
682 if (auto type_system_sp
= GetTypeSystem())
683 return type_system_sp
->GetPointeeType(m_type
);
685 return CompilerType();
688 CompilerType
CompilerType::GetPointerType() const {
690 if (auto type_system_sp
= GetTypeSystem())
691 return type_system_sp
->GetPointerType(m_type
);
693 return CompilerType();
696 CompilerType
CompilerType::AddPtrAuthModifier(uint32_t payload
) const {
698 if (auto type_system_sp
= GetTypeSystem())
699 return type_system_sp
->AddPtrAuthModifier(m_type
, payload
);
700 return CompilerType();
703 CompilerType
CompilerType::GetLValueReferenceType() const {
705 if (auto type_system_sp
= GetTypeSystem())
706 return type_system_sp
->GetLValueReferenceType(m_type
);
707 return CompilerType();
710 CompilerType
CompilerType::GetRValueReferenceType() const {
712 if (auto type_system_sp
= GetTypeSystem())
713 return type_system_sp
->GetRValueReferenceType(m_type
);
714 return CompilerType();
717 CompilerType
CompilerType::GetAtomicType() const {
719 if (auto type_system_sp
= GetTypeSystem())
720 return type_system_sp
->GetAtomicType(m_type
);
721 return CompilerType();
724 CompilerType
CompilerType::AddConstModifier() const {
726 if (auto type_system_sp
= GetTypeSystem())
727 return type_system_sp
->AddConstModifier(m_type
);
728 return CompilerType();
731 CompilerType
CompilerType::AddVolatileModifier() const {
733 if (auto type_system_sp
= GetTypeSystem())
734 return type_system_sp
->AddVolatileModifier(m_type
);
735 return CompilerType();
738 CompilerType
CompilerType::AddRestrictModifier() const {
740 if (auto type_system_sp
= GetTypeSystem())
741 return type_system_sp
->AddRestrictModifier(m_type
);
742 return CompilerType();
745 CompilerType
CompilerType::CreateTypedef(const char *name
,
746 const CompilerDeclContext
&decl_ctx
,
747 uint32_t payload
) const {
749 if (auto type_system_sp
= GetTypeSystem())
750 return type_system_sp
->CreateTypedef(m_type
, name
, decl_ctx
, payload
);
751 return CompilerType();
754 CompilerType
CompilerType::GetTypedefedType() const {
756 if (auto type_system_sp
= GetTypeSystem())
757 return type_system_sp
->GetTypedefedType(m_type
);
758 return CompilerType();
761 // Create related types using the current type's AST
764 CompilerType::GetBasicTypeFromAST(lldb::BasicType basic_type
) const {
766 if (auto type_system_sp
= GetTypeSystem())
767 return type_system_sp
->GetBasicTypeFromAST(basic_type
);
768 return CompilerType();
770 // Exploring the type
772 std::optional
<uint64_t>
773 CompilerType::GetBitSize(ExecutionContextScope
*exe_scope
) const {
775 if (auto type_system_sp
= GetTypeSystem())
776 return type_system_sp
->GetBitSize(m_type
, exe_scope
);
780 std::optional
<uint64_t>
781 CompilerType::GetByteSize(ExecutionContextScope
*exe_scope
) const {
782 if (std::optional
<uint64_t> bit_size
= GetBitSize(exe_scope
))
783 return (*bit_size
+ 7) / 8;
787 std::optional
<size_t>
788 CompilerType::GetTypeBitAlign(ExecutionContextScope
*exe_scope
) const {
790 if (auto type_system_sp
= GetTypeSystem())
791 return type_system_sp
->GetTypeBitAlign(m_type
, exe_scope
);
795 lldb::Encoding
CompilerType::GetEncoding(uint64_t &count
) const {
797 if (auto type_system_sp
= GetTypeSystem())
798 return type_system_sp
->GetEncoding(m_type
, count
);
799 return lldb::eEncodingInvalid
;
802 lldb::Format
CompilerType::GetFormat() const {
804 if (auto type_system_sp
= GetTypeSystem())
805 return type_system_sp
->GetFormat(m_type
);
806 return lldb::eFormatDefault
;
809 llvm::Expected
<uint32_t>
810 CompilerType::GetNumChildren(bool omit_empty_base_classes
,
811 const ExecutionContext
*exe_ctx
) const {
813 if (auto type_system_sp
= GetTypeSystem())
814 return type_system_sp
->GetNumChildren(m_type
, omit_empty_base_classes
,
816 return llvm::createStringError("invalid type");
819 lldb::BasicType
CompilerType::GetBasicTypeEnumeration() const {
821 if (auto type_system_sp
= GetTypeSystem())
822 return type_system_sp
->GetBasicTypeEnumeration(m_type
);
823 return eBasicTypeInvalid
;
826 void CompilerType::ForEachEnumerator(
827 std::function
<bool(const CompilerType
&integer_type
,
829 const llvm::APSInt
&value
)> const &callback
) const {
831 if (auto type_system_sp
= GetTypeSystem())
832 return type_system_sp
->ForEachEnumerator(m_type
, callback
);
835 uint32_t CompilerType::GetNumFields() const {
837 if (auto type_system_sp
= GetTypeSystem())
838 return type_system_sp
->GetNumFields(m_type
);
842 CompilerType
CompilerType::GetFieldAtIndex(size_t idx
, std::string
&name
,
843 uint64_t *bit_offset_ptr
,
844 uint32_t *bitfield_bit_size_ptr
,
845 bool *is_bitfield_ptr
) const {
847 if (auto type_system_sp
= GetTypeSystem())
848 return type_system_sp
->GetFieldAtIndex(m_type
, idx
, name
, bit_offset_ptr
,
849 bitfield_bit_size_ptr
, is_bitfield_ptr
);
850 return CompilerType();
853 uint32_t CompilerType::GetNumDirectBaseClasses() const {
855 if (auto type_system_sp
= GetTypeSystem())
856 return type_system_sp
->GetNumDirectBaseClasses(m_type
);
860 uint32_t CompilerType::GetNumVirtualBaseClasses() const {
862 if (auto type_system_sp
= GetTypeSystem())
863 return type_system_sp
->GetNumVirtualBaseClasses(m_type
);
868 CompilerType::GetDirectBaseClassAtIndex(size_t idx
,
869 uint32_t *bit_offset_ptr
) const {
871 if (auto type_system_sp
= GetTypeSystem())
872 return type_system_sp
->GetDirectBaseClassAtIndex(m_type
, idx
,
874 return CompilerType();
878 CompilerType::GetVirtualBaseClassAtIndex(size_t idx
,
879 uint32_t *bit_offset_ptr
) const {
881 if (auto type_system_sp
= GetTypeSystem())
882 return type_system_sp
->GetVirtualBaseClassAtIndex(m_type
, idx
,
884 return CompilerType();
887 CompilerDecl
CompilerType::GetStaticFieldWithName(llvm::StringRef name
) const {
889 return GetTypeSystem()->GetStaticFieldWithName(m_type
, name
);
890 return CompilerDecl();
893 uint32_t CompilerType::GetIndexOfFieldWithName(
894 const char *name
, CompilerType
*field_compiler_type_ptr
,
895 uint64_t *bit_offset_ptr
, uint32_t *bitfield_bit_size_ptr
,
896 bool *is_bitfield_ptr
) const {
897 unsigned count
= GetNumFields();
898 std::string field_name
;
899 for (unsigned index
= 0; index
< count
; index
++) {
900 CompilerType
field_compiler_type(
901 GetFieldAtIndex(index
, field_name
, bit_offset_ptr
,
902 bitfield_bit_size_ptr
, is_bitfield_ptr
));
903 if (strcmp(field_name
.c_str(), name
) == 0) {
904 if (field_compiler_type_ptr
)
905 *field_compiler_type_ptr
= field_compiler_type
;
912 llvm::Expected
<CompilerType
> CompilerType::GetChildCompilerTypeAtIndex(
913 ExecutionContext
*exe_ctx
, size_t idx
, bool transparent_pointers
,
914 bool omit_empty_base_classes
, bool ignore_array_bounds
,
915 std::string
&child_name
, uint32_t &child_byte_size
,
916 int32_t &child_byte_offset
, uint32_t &child_bitfield_bit_size
,
917 uint32_t &child_bitfield_bit_offset
, bool &child_is_base_class
,
918 bool &child_is_deref_of_parent
, ValueObject
*valobj
,
919 uint64_t &language_flags
) const {
921 if (auto type_system_sp
= GetTypeSystem())
922 return type_system_sp
->GetChildCompilerTypeAtIndex(
923 m_type
, exe_ctx
, idx
, transparent_pointers
, omit_empty_base_classes
,
924 ignore_array_bounds
, child_name
, child_byte_size
, child_byte_offset
,
925 child_bitfield_bit_size
, child_bitfield_bit_offset
,
926 child_is_base_class
, child_is_deref_of_parent
, valobj
,
928 return CompilerType();
931 // Look for a child member (doesn't include base classes, but it does include
932 // their members) in the type hierarchy. Returns an index path into
933 // "clang_type" on how to reach the appropriate member.
952 // If we have a clang type that describes "class C", and we wanted to looked
955 // With omit_empty_base_classes == false we would get an integer array back
956 // with: { 1, 1 } The first index 1 is the child index for "class A" within
957 // class C The second index 1 is the child index for "m_b" within class A
959 // With omit_empty_base_classes == true we would get an integer array back
960 // with: { 0, 1 } The first index 0 is the child index for "class A" within
961 // class C (since class B doesn't have any members it doesn't count) The second
962 // index 1 is the child index for "m_b" within class A
964 size_t CompilerType::GetIndexOfChildMemberWithName(
965 llvm::StringRef name
, bool omit_empty_base_classes
,
966 std::vector
<uint32_t> &child_indexes
) const {
967 if (IsValid() && !name
.empty()) {
968 if (auto type_system_sp
= GetTypeSystem())
969 return type_system_sp
->GetIndexOfChildMemberWithName(
970 m_type
, name
, omit_empty_base_classes
, child_indexes
);
976 CompilerType::GetDirectNestedTypeWithName(llvm::StringRef name
) const {
977 if (IsValid() && !name
.empty()) {
978 if (auto type_system_sp
= GetTypeSystem())
979 return type_system_sp
->GetDirectNestedTypeWithName(m_type
, name
);
981 return CompilerType();
984 size_t CompilerType::GetNumTemplateArguments(bool expand_pack
) const {
986 if (auto type_system_sp
= GetTypeSystem())
987 return type_system_sp
->GetNumTemplateArguments(m_type
, expand_pack
);
993 CompilerType::GetTemplateArgumentKind(size_t idx
, bool expand_pack
) const {
995 if (auto type_system_sp
= GetTypeSystem())
996 return type_system_sp
->GetTemplateArgumentKind(m_type
, idx
, expand_pack
);
997 return eTemplateArgumentKindNull
;
1000 CompilerType
CompilerType::GetTypeTemplateArgument(size_t idx
,
1001 bool expand_pack
) const {
1003 if (auto type_system_sp
= GetTypeSystem())
1004 return type_system_sp
->GetTypeTemplateArgument(m_type
, idx
, expand_pack
);
1006 return CompilerType();
1009 std::optional
<CompilerType::IntegralTemplateArgument
>
1010 CompilerType::GetIntegralTemplateArgument(size_t idx
, bool expand_pack
) const {
1012 if (auto type_system_sp
= GetTypeSystem())
1013 return type_system_sp
->GetIntegralTemplateArgument(m_type
, idx
, expand_pack
);
1014 return std::nullopt
;
1017 CompilerType
CompilerType::GetTypeForFormatters() const {
1019 if (auto type_system_sp
= GetTypeSystem())
1020 return type_system_sp
->GetTypeForFormatters(m_type
);
1021 return CompilerType();
1024 LazyBool
CompilerType::ShouldPrintAsOneLiner(ValueObject
*valobj
) const {
1026 if (auto type_system_sp
= GetTypeSystem())
1027 return type_system_sp
->ShouldPrintAsOneLiner(m_type
, valobj
);
1028 return eLazyBoolCalculate
;
1031 bool CompilerType::IsMeaninglessWithoutDynamicResolution() const {
1033 if (auto type_system_sp
= GetTypeSystem())
1034 return type_system_sp
->IsMeaninglessWithoutDynamicResolution(m_type
);
1038 // Get the index of the child of "clang_type" whose name matches. This function
1039 // doesn't descend into the children, but only looks one level deep and name
1040 // matches can include base class names.
1043 CompilerType::GetIndexOfChildWithName(llvm::StringRef name
,
1044 bool omit_empty_base_classes
) const {
1045 if (IsValid() && !name
.empty()) {
1046 if (auto type_system_sp
= GetTypeSystem())
1047 return type_system_sp
->GetIndexOfChildWithName(m_type
, name
,
1048 omit_empty_base_classes
);
1055 bool CompilerType::DumpTypeValue(Stream
*s
, lldb::Format format
,
1056 const DataExtractor
&data
,
1057 lldb::offset_t byte_offset
, size_t byte_size
,
1058 uint32_t bitfield_bit_size
,
1059 uint32_t bitfield_bit_offset
,
1060 ExecutionContextScope
*exe_scope
) {
1062 if (auto type_system_sp
= GetTypeSystem())
1063 return type_system_sp
->DumpTypeValue(
1064 m_type
, *s
, format
, data
, byte_offset
, byte_size
, bitfield_bit_size
,
1065 bitfield_bit_offset
, exe_scope
);
1069 void CompilerType::DumpTypeDescription(lldb::DescriptionLevel level
) const {
1071 if (auto type_system_sp
= GetTypeSystem())
1072 type_system_sp
->DumpTypeDescription(m_type
, level
);
1075 void CompilerType::DumpTypeDescription(Stream
*s
,
1076 lldb::DescriptionLevel level
) const {
1078 if (auto type_system_sp
= GetTypeSystem())
1079 type_system_sp
->DumpTypeDescription(m_type
, *s
, level
);
1083 LLVM_DUMP_METHOD
void CompilerType::dump() const {
1085 if (auto type_system_sp
= GetTypeSystem())
1086 return type_system_sp
->dump(m_type
);
1087 llvm::errs() << "<invalid>\n";
1091 bool CompilerType::GetValueAsScalar(const lldb_private::DataExtractor
&data
,
1092 lldb::offset_t data_byte_offset
,
1093 size_t data_byte_size
, Scalar
&value
,
1094 ExecutionContextScope
*exe_scope
) const {
1098 if (IsAggregateType()) {
1099 return false; // Aggregate types don't have scalar values
1102 lldb::Encoding encoding
= GetEncoding(count
);
1104 if (encoding
== lldb::eEncodingInvalid
|| count
!= 1)
1107 std::optional
<uint64_t> byte_size
= GetByteSize(exe_scope
);
1110 lldb::offset_t offset
= data_byte_offset
;
1112 case lldb::eEncodingInvalid
:
1114 case lldb::eEncodingVector
:
1116 case lldb::eEncodingUint
:
1117 if (*byte_size
<= sizeof(unsigned long long)) {
1118 uint64_t uval64
= data
.GetMaxU64(&offset
, *byte_size
);
1119 if (*byte_size
<= sizeof(unsigned int)) {
1120 value
= (unsigned int)uval64
;
1122 } else if (*byte_size
<= sizeof(unsigned long)) {
1123 value
= (unsigned long)uval64
;
1125 } else if (*byte_size
<= sizeof(unsigned long long)) {
1126 value
= (unsigned long long)uval64
;
1133 case lldb::eEncodingSint
:
1134 if (*byte_size
<= sizeof(long long)) {
1135 int64_t sval64
= data
.GetMaxS64(&offset
, *byte_size
);
1136 if (*byte_size
<= sizeof(int)) {
1137 value
= (int)sval64
;
1139 } else if (*byte_size
<= sizeof(long)) {
1140 value
= (long)sval64
;
1142 } else if (*byte_size
<= sizeof(long long)) {
1143 value
= (long long)sval64
;
1150 case lldb::eEncodingIEEE754
:
1151 if (*byte_size
<= sizeof(long double)) {
1154 if (*byte_size
== sizeof(float)) {
1155 if (sizeof(float) == sizeof(uint32_t)) {
1156 u32
= data
.GetU32(&offset
);
1157 value
= *((float *)&u32
);
1159 } else if (sizeof(float) == sizeof(uint64_t)) {
1160 u64
= data
.GetU64(&offset
);
1161 value
= *((float *)&u64
);
1164 } else if (*byte_size
== sizeof(double)) {
1165 if (sizeof(double) == sizeof(uint32_t)) {
1166 u32
= data
.GetU32(&offset
);
1167 value
= *((double *)&u32
);
1169 } else if (sizeof(double) == sizeof(uint64_t)) {
1170 u64
= data
.GetU64(&offset
);
1171 value
= *((double *)&u64
);
1174 } else if (*byte_size
== sizeof(long double)) {
1175 if (sizeof(long double) == sizeof(uint32_t)) {
1176 u32
= data
.GetU32(&offset
);
1177 value
= *((long double *)&u32
);
1179 } else if (sizeof(long double) == sizeof(uint64_t)) {
1180 u64
= data
.GetU64(&offset
);
1181 value
= *((long double *)&u64
);
1192 CompilerType::CompilerType(CompilerType::TypeSystemSPWrapper type_system
,
1193 lldb::opaque_compiler_type_t type
)
1194 : m_type_system(type_system
.GetSharedPointer()), m_type(type
) {
1195 assert(Verify() && "verification failed");
1198 CompilerType::CompilerType(lldb::TypeSystemWP type_system
,
1199 lldb::opaque_compiler_type_t type
)
1200 : m_type_system(type_system
), m_type(type
) {
1201 assert(Verify() && "verification failed");
1205 bool CompilerType::Verify() const {
1208 if (auto type_system_sp
= GetTypeSystem())
1209 return type_system_sp
->Verify(m_type
);
1214 CompilerType::TypeSystemSPWrapper
CompilerType::GetTypeSystem() const {
1215 return {m_type_system
.lock()};
1218 bool CompilerType::TypeSystemSPWrapper::operator==(
1219 const CompilerType::TypeSystemSPWrapper
&other
) const {
1220 if (!m_typesystem_sp
&& !other
.m_typesystem_sp
)
1222 if (m_typesystem_sp
&& other
.m_typesystem_sp
)
1223 return m_typesystem_sp
.get() == other
.m_typesystem_sp
.get();
1227 TypeSystem
*CompilerType::TypeSystemSPWrapper::operator->() const {
1228 assert(m_typesystem_sp
);
1229 return m_typesystem_sp
.get();
1232 bool lldb_private::operator==(const lldb_private::CompilerType
&lhs
,
1233 const lldb_private::CompilerType
&rhs
) {
1234 return lhs
.GetTypeSystem() == rhs
.GetTypeSystem() &&
1235 lhs
.GetOpaqueQualType() == rhs
.GetOpaqueQualType();
1238 bool lldb_private::operator!=(const lldb_private::CompilerType
&lhs
,
1239 const lldb_private::CompilerType
&rhs
) {
1240 return !(lhs
== rhs
);