1 //===-- SBType.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/API/SBType.h"
10 #include "lldb/API/SBDefines.h"
11 #include "lldb/API/SBModule.h"
12 #include "lldb/API/SBStream.h"
13 #include "lldb/API/SBTypeEnumMember.h"
14 #include "lldb/Core/Mangled.h"
15 #include "lldb/Symbol/CompilerType.h"
16 #include "lldb/Symbol/Type.h"
17 #include "lldb/Symbol/TypeSystem.h"
18 #include "lldb/Utility/ConstString.h"
19 #include "lldb/Utility/Instrumentation.h"
20 #include "lldb/Utility/Stream.h"
22 #include "llvm/ADT/APSInt.h"
27 using namespace lldb_private;
29 SBType::SBType() { LLDB_INSTRUMENT_VA(this); }
31 SBType::SBType(const CompilerType &type)
32 : m_opaque_sp(new TypeImpl(
33 CompilerType(type.GetTypeSystem(), type.GetOpaqueQualType()))) {}
35 SBType::SBType(const lldb::TypeSP &type_sp)
36 : m_opaque_sp(new TypeImpl(type_sp)) {}
38 SBType::SBType(const lldb::TypeImplSP &type_impl_sp)
39 : m_opaque_sp(type_impl_sp) {}
41 SBType::SBType(const SBType &rhs) {
42 LLDB_INSTRUMENT_VA(this, rhs);
45 m_opaque_sp = rhs.m_opaque_sp;
49 // SBType::SBType (TypeImpl* impl) :
53 bool SBType::operator==(SBType &rhs) {
54 LLDB_INSTRUMENT_VA(this, rhs);
57 return !rhs.IsValid();
62 return *m_opaque_sp.get() == *rhs.m_opaque_sp.get();
65 bool SBType::operator!=(SBType &rhs) {
66 LLDB_INSTRUMENT_VA(this, rhs);
74 return *m_opaque_sp.get() != *rhs.m_opaque_sp.get();
77 lldb::TypeImplSP SBType::GetSP() { return m_opaque_sp; }
79 void SBType::SetSP(const lldb::TypeImplSP &type_impl_sp) {
80 m_opaque_sp = type_impl_sp;
83 SBType &SBType::operator=(const SBType &rhs) {
84 LLDB_INSTRUMENT_VA(this, rhs);
87 m_opaque_sp = rhs.m_opaque_sp;
92 SBType::~SBType() = default;
94 TypeImpl &SBType::ref() {
95 if (m_opaque_sp.get() == nullptr)
96 m_opaque_sp = std::make_shared<TypeImpl>();
100 const TypeImpl &SBType::ref() const {
101 // "const SBAddress &addr" should already have checked "addr.IsValid()" prior
102 // to calling this function. In case you didn't we will assert and die to let
104 assert(m_opaque_sp.get());
108 bool SBType::IsValid() const {
109 LLDB_INSTRUMENT_VA(this);
110 return this->operator bool();
112 SBType::operator bool() const {
113 LLDB_INSTRUMENT_VA(this);
115 if (m_opaque_sp.get() == nullptr)
118 return m_opaque_sp->IsValid();
121 uint64_t SBType::GetByteSize() {
122 LLDB_INSTRUMENT_VA(this);
125 if (llvm::Optional<uint64_t> size =
126 m_opaque_sp->GetCompilerType(false).GetByteSize(nullptr))
131 bool SBType::IsPointerType() {
132 LLDB_INSTRUMENT_VA(this);
136 return m_opaque_sp->GetCompilerType(true).IsPointerType();
139 bool SBType::IsArrayType() {
140 LLDB_INSTRUMENT_VA(this);
144 return m_opaque_sp->GetCompilerType(true).IsArrayType(nullptr, nullptr,
148 bool SBType::IsVectorType() {
149 LLDB_INSTRUMENT_VA(this);
153 return m_opaque_sp->GetCompilerType(true).IsVectorType(nullptr, nullptr);
156 bool SBType::IsReferenceType() {
157 LLDB_INSTRUMENT_VA(this);
161 return m_opaque_sp->GetCompilerType(true).IsReferenceType();
164 SBType SBType::GetPointerType() {
165 LLDB_INSTRUMENT_VA(this);
170 return SBType(TypeImplSP(new TypeImpl(m_opaque_sp->GetPointerType())));
173 SBType SBType::GetPointeeType() {
174 LLDB_INSTRUMENT_VA(this);
178 return SBType(TypeImplSP(new TypeImpl(m_opaque_sp->GetPointeeType())));
181 SBType SBType::GetReferenceType() {
182 LLDB_INSTRUMENT_VA(this);
186 return SBType(TypeImplSP(new TypeImpl(m_opaque_sp->GetReferenceType())));
189 SBType SBType::GetTypedefedType() {
190 LLDB_INSTRUMENT_VA(this);
194 return SBType(TypeImplSP(new TypeImpl(m_opaque_sp->GetTypedefedType())));
197 SBType SBType::GetDereferencedType() {
198 LLDB_INSTRUMENT_VA(this);
202 return SBType(TypeImplSP(new TypeImpl(m_opaque_sp->GetDereferencedType())));
205 SBType SBType::GetArrayElementType() {
206 LLDB_INSTRUMENT_VA(this);
210 return SBType(TypeImplSP(new TypeImpl(
211 m_opaque_sp->GetCompilerType(true).GetArrayElementType(nullptr))));
214 SBType SBType::GetArrayType(uint64_t size) {
215 LLDB_INSTRUMENT_VA(this, size);
219 return SBType(TypeImplSP(
220 new TypeImpl(m_opaque_sp->GetCompilerType(true).GetArrayType(size))));
223 SBType SBType::GetVectorElementType() {
224 LLDB_INSTRUMENT_VA(this);
228 CompilerType vector_element_type;
229 if (m_opaque_sp->GetCompilerType(true).IsVectorType(&vector_element_type,
231 type_sb.SetSP(TypeImplSP(new TypeImpl(vector_element_type)));
236 bool SBType::IsFunctionType() {
237 LLDB_INSTRUMENT_VA(this);
241 return m_opaque_sp->GetCompilerType(true).IsFunctionType();
244 bool SBType::IsPolymorphicClass() {
245 LLDB_INSTRUMENT_VA(this);
249 return m_opaque_sp->GetCompilerType(true).IsPolymorphicClass();
252 bool SBType::IsTypedefType() {
253 LLDB_INSTRUMENT_VA(this);
257 return m_opaque_sp->GetCompilerType(true).IsTypedefType();
260 bool SBType::IsAnonymousType() {
261 LLDB_INSTRUMENT_VA(this);
265 return m_opaque_sp->GetCompilerType(true).IsAnonymousType();
268 bool SBType::IsScopedEnumerationType() {
269 LLDB_INSTRUMENT_VA(this);
273 return m_opaque_sp->GetCompilerType(true).IsScopedEnumerationType();
276 lldb::SBType SBType::GetFunctionReturnType() {
277 LLDB_INSTRUMENT_VA(this);
280 CompilerType return_type(
281 m_opaque_sp->GetCompilerType(true).GetFunctionReturnType());
282 if (return_type.IsValid())
283 return SBType(return_type);
285 return lldb::SBType();
288 lldb::SBTypeList SBType::GetFunctionArgumentTypes() {
289 LLDB_INSTRUMENT_VA(this);
291 SBTypeList sb_type_list;
293 CompilerType func_type(m_opaque_sp->GetCompilerType(true));
294 size_t count = func_type.GetNumberOfFunctionArguments();
295 for (size_t i = 0; i < count; i++) {
296 sb_type_list.Append(SBType(func_type.GetFunctionArgumentAtIndex(i)));
302 uint32_t SBType::GetNumberOfMemberFunctions() {
303 LLDB_INSTRUMENT_VA(this);
306 return m_opaque_sp->GetCompilerType(true).GetNumMemberFunctions();
311 lldb::SBTypeMemberFunction SBType::GetMemberFunctionAtIndex(uint32_t idx) {
312 LLDB_INSTRUMENT_VA(this, idx);
314 SBTypeMemberFunction sb_func_type;
316 sb_func_type.reset(new TypeMemberFunctionImpl(
317 m_opaque_sp->GetCompilerType(true).GetMemberFunctionAtIndex(idx)));
321 lldb::SBType SBType::GetUnqualifiedType() {
322 LLDB_INSTRUMENT_VA(this);
326 return SBType(TypeImplSP(new TypeImpl(m_opaque_sp->GetUnqualifiedType())));
329 lldb::SBType SBType::GetCanonicalType() {
330 LLDB_INSTRUMENT_VA(this);
333 return SBType(TypeImplSP(new TypeImpl(m_opaque_sp->GetCanonicalType())));
337 SBType SBType::GetEnumerationIntegerType() {
338 LLDB_INSTRUMENT_VA(this);
342 m_opaque_sp->GetCompilerType(true).GetEnumerationIntegerType());
347 lldb::BasicType SBType::GetBasicType() {
348 LLDB_INSTRUMENT_VA(this);
351 return m_opaque_sp->GetCompilerType(false).GetBasicTypeEnumeration();
352 return eBasicTypeInvalid;
355 SBType SBType::GetBasicType(lldb::BasicType basic_type) {
356 LLDB_INSTRUMENT_VA(this, basic_type);
358 if (IsValid() && m_opaque_sp->IsValid())
360 m_opaque_sp->GetTypeSystem(false)->GetBasicTypeFromAST(basic_type));
364 uint32_t SBType::GetNumberOfDirectBaseClasses() {
365 LLDB_INSTRUMENT_VA(this);
368 return m_opaque_sp->GetCompilerType(true).GetNumDirectBaseClasses();
372 uint32_t SBType::GetNumberOfVirtualBaseClasses() {
373 LLDB_INSTRUMENT_VA(this);
376 return m_opaque_sp->GetCompilerType(true).GetNumVirtualBaseClasses();
380 uint32_t SBType::GetNumberOfFields() {
381 LLDB_INSTRUMENT_VA(this);
384 return m_opaque_sp->GetCompilerType(true).GetNumFields();
388 bool SBType::GetDescription(SBStream &description,
389 lldb::DescriptionLevel description_level) {
390 LLDB_INSTRUMENT_VA(this, description, description_level);
392 Stream &strm = description.ref();
395 m_opaque_sp->GetDescription(strm, description_level);
397 strm.PutCString("No value");
402 SBTypeMember SBType::GetDirectBaseClassAtIndex(uint32_t idx) {
403 LLDB_INSTRUMENT_VA(this, idx);
405 SBTypeMember sb_type_member;
407 uint32_t bit_offset = 0;
408 CompilerType base_class_type =
409 m_opaque_sp->GetCompilerType(true).GetDirectBaseClassAtIndex(
411 if (base_class_type.IsValid())
412 sb_type_member.reset(new TypeMemberImpl(
413 TypeImplSP(new TypeImpl(base_class_type)), bit_offset));
415 return sb_type_member;
418 SBTypeMember SBType::GetVirtualBaseClassAtIndex(uint32_t idx) {
419 LLDB_INSTRUMENT_VA(this, idx);
421 SBTypeMember sb_type_member;
423 uint32_t bit_offset = 0;
424 CompilerType base_class_type =
425 m_opaque_sp->GetCompilerType(true).GetVirtualBaseClassAtIndex(
427 if (base_class_type.IsValid())
428 sb_type_member.reset(new TypeMemberImpl(
429 TypeImplSP(new TypeImpl(base_class_type)), bit_offset));
431 return sb_type_member;
434 SBTypeEnumMemberList SBType::GetEnumMembers() {
435 LLDB_INSTRUMENT_VA(this);
437 SBTypeEnumMemberList sb_enum_member_list;
439 CompilerType this_type(m_opaque_sp->GetCompilerType(true));
440 if (this_type.IsValid()) {
441 this_type.ForEachEnumerator([&sb_enum_member_list](
442 const CompilerType &integer_type,
444 const llvm::APSInt &value) -> bool {
445 SBTypeEnumMember enum_member(
446 lldb::TypeEnumMemberImplSP(new TypeEnumMemberImpl(
447 lldb::TypeImplSP(new TypeImpl(integer_type)), name, value)));
448 sb_enum_member_list.Append(enum_member);
449 return true; // Keep iterating
453 return sb_enum_member_list;
456 SBTypeMember SBType::GetFieldAtIndex(uint32_t idx) {
457 LLDB_INSTRUMENT_VA(this, idx);
459 SBTypeMember sb_type_member;
461 CompilerType this_type(m_opaque_sp->GetCompilerType(false));
462 if (this_type.IsValid()) {
463 uint64_t bit_offset = 0;
464 uint32_t bitfield_bit_size = 0;
465 bool is_bitfield = false;
466 std::string name_sstr;
467 CompilerType field_type(this_type.GetFieldAtIndex(
468 idx, name_sstr, &bit_offset, &bitfield_bit_size, &is_bitfield));
469 if (field_type.IsValid()) {
471 if (!name_sstr.empty())
472 name.SetCString(name_sstr.c_str());
473 sb_type_member.reset(
474 new TypeMemberImpl(TypeImplSP(new TypeImpl(field_type)), bit_offset,
475 name, bitfield_bit_size, is_bitfield));
479 return sb_type_member;
482 bool SBType::IsTypeComplete() {
483 LLDB_INSTRUMENT_VA(this);
487 return m_opaque_sp->GetCompilerType(false).IsCompleteType();
490 uint32_t SBType::GetTypeFlags() {
491 LLDB_INSTRUMENT_VA(this);
495 return m_opaque_sp->GetCompilerType(true).GetTypeInfo();
498 lldb::SBModule SBType::GetModule() {
499 LLDB_INSTRUMENT_VA(this);
501 lldb::SBModule sb_module;
505 sb_module.SetSP(m_opaque_sp->GetModule());
509 const char *SBType::GetName() {
510 LLDB_INSTRUMENT_VA(this);
514 return m_opaque_sp->GetName().GetCString();
517 const char *SBType::GetDisplayTypeName() {
518 LLDB_INSTRUMENT_VA(this);
522 return m_opaque_sp->GetDisplayTypeName().GetCString();
525 lldb::TypeClass SBType::GetTypeClass() {
526 LLDB_INSTRUMENT_VA(this);
529 return m_opaque_sp->GetCompilerType(true).GetTypeClass();
530 return lldb::eTypeClassInvalid;
533 uint32_t SBType::GetNumberOfTemplateArguments() {
534 LLDB_INSTRUMENT_VA(this);
537 return m_opaque_sp->GetCompilerType(false).GetNumTemplateArguments();
541 lldb::SBType SBType::GetTemplateArgumentType(uint32_t idx) {
542 LLDB_INSTRUMENT_VA(this, idx);
548 switch(GetTemplateArgumentKind(idx)) {
549 case eTemplateArgumentKindType:
550 type = m_opaque_sp->GetCompilerType(false).GetTypeTemplateArgument(idx);
552 case eTemplateArgumentKindIntegral:
553 type = m_opaque_sp->GetCompilerType(false)
554 .GetIntegralTemplateArgument(idx)
565 lldb::TemplateArgumentKind SBType::GetTemplateArgumentKind(uint32_t idx) {
566 LLDB_INSTRUMENT_VA(this, idx);
569 return m_opaque_sp->GetCompilerType(false).GetTemplateArgumentKind(idx);
570 return eTemplateArgumentKindNull;
573 SBTypeList::SBTypeList() : m_opaque_up(new TypeListImpl()) {
574 LLDB_INSTRUMENT_VA(this);
577 SBTypeList::SBTypeList(const SBTypeList &rhs)
578 : m_opaque_up(new TypeListImpl()) {
579 LLDB_INSTRUMENT_VA(this, rhs);
581 for (uint32_t i = 0, rhs_size = const_cast<SBTypeList &>(rhs).GetSize();
583 Append(const_cast<SBTypeList &>(rhs).GetTypeAtIndex(i));
586 bool SBTypeList::IsValid() {
587 LLDB_INSTRUMENT_VA(this);
588 return this->operator bool();
590 SBTypeList::operator bool() const {
591 LLDB_INSTRUMENT_VA(this);
593 return (m_opaque_up != nullptr);
596 SBTypeList &SBTypeList::operator=(const SBTypeList &rhs) {
597 LLDB_INSTRUMENT_VA(this, rhs);
600 m_opaque_up = std::make_unique<TypeListImpl>();
601 for (uint32_t i = 0, rhs_size = const_cast<SBTypeList &>(rhs).GetSize();
603 Append(const_cast<SBTypeList &>(rhs).GetTypeAtIndex(i));
608 void SBTypeList::Append(SBType type) {
609 LLDB_INSTRUMENT_VA(this, type);
612 m_opaque_up->Append(type.m_opaque_sp);
615 SBType SBTypeList::GetTypeAtIndex(uint32_t index) {
616 LLDB_INSTRUMENT_VA(this, index);
619 return SBType(m_opaque_up->GetTypeAtIndex(index));
623 uint32_t SBTypeList::GetSize() {
624 LLDB_INSTRUMENT_VA(this);
626 return m_opaque_up->GetSize();
629 SBTypeList::~SBTypeList() = default;
631 SBTypeMember::SBTypeMember() { LLDB_INSTRUMENT_VA(this); }
633 SBTypeMember::~SBTypeMember() = default;
635 SBTypeMember::SBTypeMember(const SBTypeMember &rhs) {
636 LLDB_INSTRUMENT_VA(this, rhs);
640 m_opaque_up = std::make_unique<TypeMemberImpl>(rhs.ref());
644 lldb::SBTypeMember &SBTypeMember::operator=(const lldb::SBTypeMember &rhs) {
645 LLDB_INSTRUMENT_VA(this, rhs);
649 m_opaque_up = std::make_unique<TypeMemberImpl>(rhs.ref());
654 bool SBTypeMember::IsValid() const {
655 LLDB_INSTRUMENT_VA(this);
656 return this->operator bool();
658 SBTypeMember::operator bool() const {
659 LLDB_INSTRUMENT_VA(this);
661 return m_opaque_up.get();
664 const char *SBTypeMember::GetName() {
665 LLDB_INSTRUMENT_VA(this);
668 return m_opaque_up->GetName().GetCString();
672 SBType SBTypeMember::GetType() {
673 LLDB_INSTRUMENT_VA(this);
677 sb_type.SetSP(m_opaque_up->GetTypeImpl());
682 uint64_t SBTypeMember::GetOffsetInBytes() {
683 LLDB_INSTRUMENT_VA(this);
686 return m_opaque_up->GetBitOffset() / 8u;
690 uint64_t SBTypeMember::GetOffsetInBits() {
691 LLDB_INSTRUMENT_VA(this);
694 return m_opaque_up->GetBitOffset();
698 bool SBTypeMember::IsBitfield() {
699 LLDB_INSTRUMENT_VA(this);
702 return m_opaque_up->GetIsBitfield();
706 uint32_t SBTypeMember::GetBitfieldSizeInBits() {
707 LLDB_INSTRUMENT_VA(this);
710 return m_opaque_up->GetBitfieldBitSize();
714 bool SBTypeMember::GetDescription(lldb::SBStream &description,
715 lldb::DescriptionLevel description_level) {
716 LLDB_INSTRUMENT_VA(this, description, description_level);
718 Stream &strm = description.ref();
721 const uint32_t bit_offset = m_opaque_up->GetBitOffset();
722 const uint32_t byte_offset = bit_offset / 8u;
723 const uint32_t byte_bit_offset = bit_offset % 8u;
724 const char *name = m_opaque_up->GetName().GetCString();
726 strm.Printf("+%u + %u bits: (", byte_offset, byte_bit_offset);
728 strm.Printf("+%u: (", byte_offset);
730 TypeImplSP type_impl_sp(m_opaque_up->GetTypeImpl());
732 type_impl_sp->GetDescription(strm, description_level);
734 strm.Printf(") %s", name);
735 if (m_opaque_up->GetIsBitfield()) {
736 const uint32_t bitfield_bit_size = m_opaque_up->GetBitfieldBitSize();
737 strm.Printf(" : %u", bitfield_bit_size);
740 strm.PutCString("No value");
745 void SBTypeMember::reset(TypeMemberImpl *type_member_impl) {
746 m_opaque_up.reset(type_member_impl);
749 TypeMemberImpl &SBTypeMember::ref() {
750 if (m_opaque_up == nullptr)
751 m_opaque_up = std::make_unique<TypeMemberImpl>();
755 const TypeMemberImpl &SBTypeMember::ref() const { return *m_opaque_up; }
757 SBTypeMemberFunction::SBTypeMemberFunction() { LLDB_INSTRUMENT_VA(this); }
759 SBTypeMemberFunction::~SBTypeMemberFunction() = default;
761 SBTypeMemberFunction::SBTypeMemberFunction(const SBTypeMemberFunction &rhs)
762 : m_opaque_sp(rhs.m_opaque_sp) {
763 LLDB_INSTRUMENT_VA(this, rhs);
766 lldb::SBTypeMemberFunction &SBTypeMemberFunction::
767 operator=(const lldb::SBTypeMemberFunction &rhs) {
768 LLDB_INSTRUMENT_VA(this, rhs);
771 m_opaque_sp = rhs.m_opaque_sp;
775 bool SBTypeMemberFunction::IsValid() const {
776 LLDB_INSTRUMENT_VA(this);
777 return this->operator bool();
779 SBTypeMemberFunction::operator bool() const {
780 LLDB_INSTRUMENT_VA(this);
782 return m_opaque_sp.get();
785 const char *SBTypeMemberFunction::GetName() {
786 LLDB_INSTRUMENT_VA(this);
789 return m_opaque_sp->GetName().GetCString();
793 const char *SBTypeMemberFunction::GetDemangledName() {
794 LLDB_INSTRUMENT_VA(this);
797 ConstString mangled_str = m_opaque_sp->GetMangledName();
799 Mangled mangled(mangled_str);
800 return mangled.GetDemangledName().GetCString();
806 const char *SBTypeMemberFunction::GetMangledName() {
807 LLDB_INSTRUMENT_VA(this);
810 return m_opaque_sp->GetMangledName().GetCString();
814 SBType SBTypeMemberFunction::GetType() {
815 LLDB_INSTRUMENT_VA(this);
819 sb_type.SetSP(lldb::TypeImplSP(new TypeImpl(m_opaque_sp->GetType())));
824 lldb::SBType SBTypeMemberFunction::GetReturnType() {
825 LLDB_INSTRUMENT_VA(this);
829 sb_type.SetSP(lldb::TypeImplSP(new TypeImpl(m_opaque_sp->GetReturnType())));
834 uint32_t SBTypeMemberFunction::GetNumberOfArguments() {
835 LLDB_INSTRUMENT_VA(this);
838 return m_opaque_sp->GetNumArguments();
842 lldb::SBType SBTypeMemberFunction::GetArgumentTypeAtIndex(uint32_t i) {
843 LLDB_INSTRUMENT_VA(this, i);
848 lldb::TypeImplSP(new TypeImpl(m_opaque_sp->GetArgumentAtIndex(i))));
853 lldb::MemberFunctionKind SBTypeMemberFunction::GetKind() {
854 LLDB_INSTRUMENT_VA(this);
857 return m_opaque_sp->GetKind();
858 return lldb::eMemberFunctionKindUnknown;
861 bool SBTypeMemberFunction::GetDescription(
862 lldb::SBStream &description, lldb::DescriptionLevel description_level) {
863 LLDB_INSTRUMENT_VA(this, description, description_level);
865 Stream &strm = description.ref();
868 return m_opaque_sp->GetDescription(strm);
873 void SBTypeMemberFunction::reset(TypeMemberFunctionImpl *type_member_impl) {
874 m_opaque_sp.reset(type_member_impl);
877 TypeMemberFunctionImpl &SBTypeMemberFunction::ref() {
879 m_opaque_sp = std::make_shared<TypeMemberFunctionImpl>();
880 return *m_opaque_sp.get();
883 const TypeMemberFunctionImpl &SBTypeMemberFunction::ref() const {
884 return *m_opaque_sp.get();