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/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"
27 using namespace lldb_private
;
31 bool CompilerType::IsAggregateType() const {
33 return m_type_system
->IsAggregateType(m_type
);
37 bool CompilerType::IsAnonymousType() const {
39 return m_type_system
->IsAnonymousType(m_type
);
43 bool CompilerType::IsScopedEnumerationType() const {
45 return m_type_system
->IsScopedEnumerationType(m_type
);
49 bool CompilerType::IsArrayType(CompilerType
*element_type_ptr
, uint64_t *size
,
50 bool *is_incomplete
) const {
52 return m_type_system
->IsArrayType(m_type
, element_type_ptr
, size
,
56 element_type_ptr
->Clear();
60 *is_incomplete
= false;
64 bool CompilerType::IsVectorType(CompilerType
*element_type
,
65 uint64_t *size
) const {
67 return m_type_system
->IsVectorType(m_type
, element_type
, size
);
71 bool CompilerType::IsRuntimeGeneratedType() const {
73 return m_type_system
->IsRuntimeGeneratedType(m_type
);
77 bool CompilerType::IsCharType() const {
79 return m_type_system
->IsCharType(m_type
);
83 bool CompilerType::IsCompleteType() const {
85 return m_type_system
->IsCompleteType(m_type
);
89 bool CompilerType::IsConst() const {
91 return m_type_system
->IsConst(m_type
);
95 bool CompilerType::IsCStringType(uint32_t &length
) const {
97 return m_type_system
->IsCStringType(m_type
, length
);
101 bool CompilerType::IsFunctionType() const {
103 return m_type_system
->IsFunctionType(m_type
);
107 // Used to detect "Homogeneous Floating-point Aggregates"
109 CompilerType::IsHomogeneousAggregate(CompilerType
*base_type_ptr
) const {
111 return m_type_system
->IsHomogeneousAggregate(m_type
, base_type_ptr
);
115 size_t CompilerType::GetNumberOfFunctionArguments() const {
117 return m_type_system
->GetNumberOfFunctionArguments(m_type
);
122 CompilerType::GetFunctionArgumentAtIndex(const size_t index
) const {
124 return m_type_system
->GetFunctionArgumentAtIndex(m_type
, index
);
125 return CompilerType();
128 bool CompilerType::IsFunctionPointerType() const {
130 return m_type_system
->IsFunctionPointerType(m_type
);
134 bool CompilerType::IsBlockPointerType(
135 CompilerType
*function_pointer_type_ptr
) const {
137 return m_type_system
->IsBlockPointerType(m_type
, function_pointer_type_ptr
);
141 bool CompilerType::IsIntegerType(bool &is_signed
) const {
143 return m_type_system
->IsIntegerType(m_type
, is_signed
);
147 bool CompilerType::IsEnumerationType(bool &is_signed
) const {
149 return m_type_system
->IsEnumerationType(m_type
, is_signed
);
153 bool CompilerType::IsIntegerOrEnumerationType(bool &is_signed
) const {
154 return IsIntegerType(is_signed
) || IsEnumerationType(is_signed
);
157 bool CompilerType::IsPointerType(CompilerType
*pointee_type
) const {
159 return m_type_system
->IsPointerType(m_type
, pointee_type
);
162 pointee_type
->Clear();
166 bool CompilerType::IsPointerOrReferenceType(CompilerType
*pointee_type
) const {
168 return m_type_system
->IsPointerOrReferenceType(m_type
, pointee_type
);
171 pointee_type
->Clear();
175 bool CompilerType::IsReferenceType(CompilerType
*pointee_type
,
176 bool *is_rvalue
) const {
178 return m_type_system
->IsReferenceType(m_type
, pointee_type
, is_rvalue
);
181 pointee_type
->Clear();
185 bool CompilerType::ShouldTreatScalarValueAsAddress() const {
187 return m_type_system
->ShouldTreatScalarValueAsAddress(m_type
);
191 bool CompilerType::IsFloatingPointType(uint32_t &count
,
192 bool &is_complex
) const {
194 return m_type_system
->IsFloatingPointType(m_type
, count
, is_complex
);
201 bool CompilerType::IsDefined() const {
203 return m_type_system
->IsDefined(m_type
);
207 bool CompilerType::IsPolymorphicClass() const {
209 return m_type_system
->IsPolymorphicClass(m_type
);
214 bool CompilerType::IsPossibleDynamicType(CompilerType
*dynamic_pointee_type
,
215 bool check_cplusplus
,
216 bool check_objc
) const {
218 return m_type_system
->IsPossibleDynamicType(m_type
, dynamic_pointee_type
,
219 check_cplusplus
, check_objc
);
223 bool CompilerType::IsScalarType() const {
227 return m_type_system
->IsScalarType(m_type
);
230 bool CompilerType::IsTypedefType() const {
233 return m_type_system
->IsTypedefType(m_type
);
236 bool CompilerType::IsVoidType() const {
239 return m_type_system
->IsVoidType(m_type
);
242 bool CompilerType::IsPointerToScalarType() const {
246 return IsPointerType() && GetPointeeType().IsScalarType();
249 bool CompilerType::IsArrayOfScalarType() const {
250 CompilerType element_type
;
251 if (IsArrayType(&element_type
))
252 return element_type
.IsScalarType();
256 bool CompilerType::IsBeingDefined() const {
259 return m_type_system
->IsBeingDefined(m_type
);
264 bool CompilerType::GetCompleteType() const {
267 return m_type_system
->GetCompleteType(m_type
);
270 // AST related queries
271 size_t CompilerType::GetPointerByteSize() const {
273 return m_type_system
->GetPointerByteSize();
277 ConstString
CompilerType::GetTypeName() const {
279 return m_type_system
->GetTypeName(m_type
);
281 return ConstString("<invalid>");
284 ConstString
CompilerType::GetDisplayTypeName() const {
286 return m_type_system
->GetDisplayTypeName(m_type
);
287 return ConstString("<invalid>");
290 uint32_t CompilerType::GetTypeInfo(
291 CompilerType
*pointee_or_element_compiler_type
) const {
295 return m_type_system
->GetTypeInfo(m_type
, pointee_or_element_compiler_type
);
298 lldb::LanguageType
CompilerType::GetMinimumLanguage() {
300 return lldb::eLanguageTypeC
;
302 return m_type_system
->GetMinimumLanguage(m_type
);
305 lldb::TypeClass
CompilerType::GetTypeClass() const {
307 return lldb::eTypeClassInvalid
;
309 return m_type_system
->GetTypeClass(m_type
);
312 void CompilerType::SetCompilerType(TypeSystem
*type_system
,
313 lldb::opaque_compiler_type_t type
) {
314 m_type_system
= type_system
;
318 unsigned CompilerType::GetTypeQualifiers() const {
320 return m_type_system
->GetTypeQualifiers(m_type
);
324 // Creating related types
327 CompilerType::GetArrayElementType(ExecutionContextScope
*exe_scope
) const {
329 return m_type_system
->GetArrayElementType(m_type
, exe_scope
);
331 return CompilerType();
334 CompilerType
CompilerType::GetArrayType(uint64_t size
) const {
336 return m_type_system
->GetArrayType(m_type
, size
);
338 return CompilerType();
341 CompilerType
CompilerType::GetCanonicalType() const {
343 return m_type_system
->GetCanonicalType(m_type
);
344 return CompilerType();
347 CompilerType
CompilerType::GetFullyUnqualifiedType() const {
349 return m_type_system
->GetFullyUnqualifiedType(m_type
);
350 return CompilerType();
353 CompilerType
CompilerType::GetEnumerationIntegerType() const {
355 return m_type_system
->GetEnumerationIntegerType(m_type
);
356 return CompilerType();
359 int CompilerType::GetFunctionArgumentCount() const {
361 return m_type_system
->GetFunctionArgumentCount(m_type
);
366 CompilerType
CompilerType::GetFunctionArgumentTypeAtIndex(size_t idx
) const {
368 return m_type_system
->GetFunctionArgumentTypeAtIndex(m_type
, idx
);
370 return CompilerType();
373 CompilerType
CompilerType::GetFunctionReturnType() const {
375 return m_type_system
->GetFunctionReturnType(m_type
);
377 return CompilerType();
380 size_t CompilerType::GetNumMemberFunctions() const {
382 return m_type_system
->GetNumMemberFunctions(m_type
);
387 TypeMemberFunctionImpl
CompilerType::GetMemberFunctionAtIndex(size_t idx
) {
389 return m_type_system
->GetMemberFunctionAtIndex(m_type
, idx
);
391 return TypeMemberFunctionImpl();
394 CompilerType
CompilerType::GetNonReferenceType() const {
396 return m_type_system
->GetNonReferenceType(m_type
);
397 return CompilerType();
400 CompilerType
CompilerType::GetPointeeType() const {
402 return m_type_system
->GetPointeeType(m_type
);
404 return CompilerType();
407 CompilerType
CompilerType::GetPointerType() const {
409 return m_type_system
->GetPointerType(m_type
);
411 return CompilerType();
414 CompilerType
CompilerType::GetLValueReferenceType() const {
416 return m_type_system
->GetLValueReferenceType(m_type
);
418 return CompilerType();
421 CompilerType
CompilerType::GetRValueReferenceType() const {
423 return m_type_system
->GetRValueReferenceType(m_type
);
425 return CompilerType();
428 CompilerType
CompilerType::GetAtomicType() const {
430 return m_type_system
->GetAtomicType(m_type
);
431 return CompilerType();
434 CompilerType
CompilerType::AddConstModifier() const {
436 return m_type_system
->AddConstModifier(m_type
);
438 return CompilerType();
441 CompilerType
CompilerType::AddVolatileModifier() const {
443 return m_type_system
->AddVolatileModifier(m_type
);
445 return CompilerType();
448 CompilerType
CompilerType::AddRestrictModifier() const {
450 return m_type_system
->AddRestrictModifier(m_type
);
452 return CompilerType();
455 CompilerType
CompilerType::CreateTypedef(const char *name
,
456 const CompilerDeclContext
&decl_ctx
,
457 uint32_t payload
) const {
459 return m_type_system
->CreateTypedef(m_type
, name
, decl_ctx
, payload
);
461 return CompilerType();
464 CompilerType
CompilerType::GetTypedefedType() const {
466 return m_type_system
->GetTypedefedType(m_type
);
468 return CompilerType();
471 // Create related types using the current type's AST
474 CompilerType::GetBasicTypeFromAST(lldb::BasicType basic_type
) const {
476 return m_type_system
->GetBasicTypeFromAST(basic_type
);
477 return CompilerType();
479 // Exploring the type
481 llvm::Optional
<uint64_t>
482 CompilerType::GetBitSize(ExecutionContextScope
*exe_scope
) const {
484 return m_type_system
->GetBitSize(m_type
, exe_scope
);
488 llvm::Optional
<uint64_t>
489 CompilerType::GetByteSize(ExecutionContextScope
*exe_scope
) const {
490 if (llvm::Optional
<uint64_t> bit_size
= GetBitSize(exe_scope
))
491 return (*bit_size
+ 7) / 8;
495 llvm::Optional
<size_t> CompilerType::GetTypeBitAlign(ExecutionContextScope
*exe_scope
) const {
497 return m_type_system
->GetTypeBitAlign(m_type
, exe_scope
);
501 lldb::Encoding
CompilerType::GetEncoding(uint64_t &count
) const {
503 return lldb::eEncodingInvalid
;
505 return m_type_system
->GetEncoding(m_type
, count
);
508 lldb::Format
CompilerType::GetFormat() const {
510 return lldb::eFormatDefault
;
512 return m_type_system
->GetFormat(m_type
);
515 uint32_t CompilerType::GetNumChildren(bool omit_empty_base_classes
,
516 const ExecutionContext
*exe_ctx
) const {
519 return m_type_system
->GetNumChildren(m_type
, omit_empty_base_classes
,
523 lldb::BasicType
CompilerType::GetBasicTypeEnumeration() const {
525 return m_type_system
->GetBasicTypeEnumeration(m_type
);
526 return eBasicTypeInvalid
;
529 void CompilerType::ForEachEnumerator(
530 std::function
<bool(const CompilerType
&integer_type
,
532 const llvm::APSInt
&value
)> const &callback
) const {
534 return m_type_system
->ForEachEnumerator(m_type
, callback
);
537 uint32_t CompilerType::GetNumFields() const {
540 return m_type_system
->GetNumFields(m_type
);
543 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 {
548 return CompilerType();
549 return m_type_system
->GetFieldAtIndex(m_type
, idx
, name
, bit_offset_ptr
,
550 bitfield_bit_size_ptr
, is_bitfield_ptr
);
553 uint32_t CompilerType::GetNumDirectBaseClasses() const {
555 return m_type_system
->GetNumDirectBaseClasses(m_type
);
559 uint32_t CompilerType::GetNumVirtualBaseClasses() const {
561 return m_type_system
->GetNumVirtualBaseClasses(m_type
);
566 CompilerType::GetDirectBaseClassAtIndex(size_t idx
,
567 uint32_t *bit_offset_ptr
) const {
569 return m_type_system
->GetDirectBaseClassAtIndex(m_type
, idx
,
571 return CompilerType();
575 CompilerType::GetVirtualBaseClassAtIndex(size_t idx
,
576 uint32_t *bit_offset_ptr
) const {
578 return m_type_system
->GetVirtualBaseClassAtIndex(m_type
, idx
,
580 return CompilerType();
583 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
;
602 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 {
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
);
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.
640 // If we have a clang type that describes "class C", and we wanted to looked
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
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
652 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
);
662 size_t CompilerType::GetNumTemplateArguments() const {
664 return m_type_system
->GetNumTemplateArguments(m_type
);
669 TemplateArgumentKind
CompilerType::GetTemplateArgumentKind(size_t idx
) const {
671 return m_type_system
->GetTemplateArgumentKind(m_type
, idx
);
672 return eTemplateArgumentKindNull
;
675 CompilerType
CompilerType::GetTypeTemplateArgument(size_t idx
) const {
677 return m_type_system
->GetTypeTemplateArgument(m_type
, idx
);
679 return CompilerType();
682 llvm::Optional
<CompilerType::IntegralTemplateArgument
>
683 CompilerType::GetIntegralTemplateArgument(size_t idx
) const {
685 return m_type_system
->GetIntegralTemplateArgument(m_type
, idx
);
689 CompilerType
CompilerType::GetTypeForFormatters() const {
691 return m_type_system
->GetTypeForFormatters(m_type
);
692 return CompilerType();
695 LazyBool
CompilerType::ShouldPrintAsOneLiner(ValueObject
*valobj
) const {
697 return m_type_system
->ShouldPrintAsOneLiner(m_type
, valobj
);
698 return eLazyBoolCalculate
;
701 bool CompilerType::IsMeaninglessWithoutDynamicResolution() const {
703 return m_type_system
->IsMeaninglessWithoutDynamicResolution(m_type
);
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.
712 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
);
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
) {
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
,
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
) {
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
);
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
) {
755 m_type_system
->DumpSummary(m_type
, exe_ctx
, s
, data
, data_byte_offset
,
759 void CompilerType::DumpTypeDescription(lldb::DescriptionLevel level
) const {
761 m_type_system
->DumpTypeDescription(m_type
, level
);
764 void CompilerType::DumpTypeDescription(Stream
*s
,
765 lldb::DescriptionLevel level
) const {
767 m_type_system
->DumpTypeDescription(m_type
, s
, level
);
772 LLVM_DUMP_METHOD
void CompilerType::dump() const {
774 m_type_system
->dump(m_type
);
776 llvm::errs() << "<invalid>\n";
780 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 {
787 if (IsAggregateType()) {
788 return false; // Aggregate types don't have scalar values
791 lldb::Encoding encoding
= GetEncoding(count
);
793 if (encoding
== lldb::eEncodingInvalid
|| count
!= 1)
796 llvm::Optional
<uint64_t> byte_size
= GetByteSize(exe_scope
);
799 lldb::offset_t offset
= data_byte_offset
;
801 case lldb::eEncodingInvalid
:
803 case lldb::eEncodingVector
:
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
;
811 } else if (*byte_size
<= sizeof(unsigned long)) {
812 value
= (unsigned long)uval64
;
814 } else if (*byte_size
<= sizeof(unsigned long long)) {
815 value
= (unsigned long long)uval64
;
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)) {
828 } else if (*byte_size
<= sizeof(long)) {
829 value
= (long)sval64
;
831 } else if (*byte_size
<= sizeof(long long)) {
832 value
= (long long)sval64
;
839 case lldb::eEncodingIEEE754
:
840 if (*byte_size
<= sizeof(long double)) {
843 if (*byte_size
== sizeof(float)) {
844 if (sizeof(float) == sizeof(uint32_t)) {
845 u32
= data
.GetU32(&offset
);
846 value
= *((float *)&u32
);
848 } else if (sizeof(float) == sizeof(uint64_t)) {
849 u64
= data
.GetU64(&offset
);
850 value
= *((float *)&u64
);
853 } else if (*byte_size
== sizeof(double)) {
854 if (sizeof(double) == sizeof(uint32_t)) {
855 u32
= data
.GetU32(&offset
);
856 value
= *((double *)&u32
);
858 } else if (sizeof(double) == sizeof(uint64_t)) {
859 u64
= data
.GetU64(&offset
);
860 value
= *((double *)&u64
);
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
);
868 } else if (sizeof(long double) == sizeof(uint64_t)) {
869 u64
= data
.GetU64(&offset
);
870 value
= *((long double *)&u64
);
882 bool CompilerType::Verify() const {
883 return !IsValid() || m_type_system
->Verify(m_type
);
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();
893 bool lldb_private::operator!=(const lldb_private::CompilerType
&lhs
,
894 const lldb_private::CompilerType
&rhs
) {
895 return !(lhs
== rhs
);