1 //===-- SBType.cpp ----------------------------------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #include "lldb/API/SBType.h"
11 #include "lldb/API/SBDefines.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/Log.h"
20 #include "lldb/Utility/Stream.h"
22 #include "llvm/ADT/APSInt.h"
25 using namespace lldb_private;
27 SBType::SBType() : m_opaque_sp() {}
29 SBType::SBType(const CompilerType &type)
30 : m_opaque_sp(new TypeImpl(
31 CompilerType(type.GetTypeSystem(), type.GetOpaqueQualType()))) {}
33 SBType::SBType(const lldb::TypeSP &type_sp)
34 : m_opaque_sp(new TypeImpl(type_sp)) {}
36 SBType::SBType(const lldb::TypeImplSP &type_impl_sp)
37 : m_opaque_sp(type_impl_sp) {}
39 SBType::SBType(const SBType &rhs) : m_opaque_sp() {
41 m_opaque_sp = rhs.m_opaque_sp;
45 // SBType::SBType (TypeImpl* impl) :
49 bool SBType::operator==(SBType &rhs) {
50 if (IsValid() == false)
51 return !rhs.IsValid();
53 if (rhs.IsValid() == false)
56 return *m_opaque_sp.get() == *rhs.m_opaque_sp.get();
59 bool SBType::operator!=(SBType &rhs) {
60 if (IsValid() == false)
63 if (rhs.IsValid() == false)
66 return *m_opaque_sp.get() != *rhs.m_opaque_sp.get();
69 lldb::TypeImplSP SBType::GetSP() { return m_opaque_sp; }
71 void SBType::SetSP(const lldb::TypeImplSP &type_impl_sp) {
72 m_opaque_sp = type_impl_sp;
75 SBType &SBType::operator=(const SBType &rhs) {
77 m_opaque_sp = rhs.m_opaque_sp;
84 TypeImpl &SBType::ref() {
85 if (m_opaque_sp.get() == NULL)
86 m_opaque_sp.reset(new TypeImpl());
90 const TypeImpl &SBType::ref() const {
91 // "const SBAddress &addr" should already have checked "addr.IsValid()"
92 // prior to calling this function. In case you didn't we will assert
93 // and die to let you know.
94 assert(m_opaque_sp.get());
98 bool SBType::IsValid() const {
99 if (m_opaque_sp.get() == NULL)
102 return m_opaque_sp->IsValid();
105 uint64_t SBType::GetByteSize() {
109 return m_opaque_sp->GetCompilerType(false).GetByteSize(nullptr);
112 bool SBType::IsPointerType() {
115 return m_opaque_sp->GetCompilerType(true).IsPointerType();
118 bool SBType::IsArrayType() {
121 return m_opaque_sp->GetCompilerType(true).IsArrayType(nullptr, nullptr,
125 bool SBType::IsVectorType() {
128 return m_opaque_sp->GetCompilerType(true).IsVectorType(nullptr, nullptr);
131 bool SBType::IsReferenceType() {
134 return m_opaque_sp->GetCompilerType(true).IsReferenceType();
137 SBType SBType::GetPointerType() {
141 return SBType(TypeImplSP(new TypeImpl(m_opaque_sp->GetPointerType())));
144 SBType SBType::GetPointeeType() {
147 return SBType(TypeImplSP(new TypeImpl(m_opaque_sp->GetPointeeType())));
150 SBType SBType::GetReferenceType() {
153 return SBType(TypeImplSP(new TypeImpl(m_opaque_sp->GetReferenceType())));
156 SBType SBType::GetTypedefedType() {
159 return SBType(TypeImplSP(new TypeImpl(m_opaque_sp->GetTypedefedType())));
162 SBType SBType::GetDereferencedType() {
165 return SBType(TypeImplSP(new TypeImpl(m_opaque_sp->GetDereferencedType())));
168 SBType SBType::GetArrayElementType() {
171 return SBType(TypeImplSP(
172 new TypeImpl(m_opaque_sp->GetCompilerType(true).GetArrayElementType())));
175 SBType SBType::GetArrayType(uint64_t size) {
178 return SBType(TypeImplSP(
179 new TypeImpl(m_opaque_sp->GetCompilerType(true).GetArrayType(size))));
182 SBType SBType::GetVectorElementType() {
185 CompilerType vector_element_type;
186 if (m_opaque_sp->GetCompilerType(true).IsVectorType(&vector_element_type,
188 type_sb.SetSP(TypeImplSP(new TypeImpl(vector_element_type)));
193 bool SBType::IsFunctionType() {
196 return m_opaque_sp->GetCompilerType(true).IsFunctionType();
199 bool SBType::IsPolymorphicClass() {
202 return m_opaque_sp->GetCompilerType(true).IsPolymorphicClass();
205 bool SBType::IsTypedefType() {
208 return m_opaque_sp->GetCompilerType(true).IsTypedefType();
211 bool SBType::IsAnonymousType() {
214 return m_opaque_sp->GetCompilerType(true).IsAnonymousType();
217 lldb::SBType SBType::GetFunctionReturnType() {
219 CompilerType return_type(
220 m_opaque_sp->GetCompilerType(true).GetFunctionReturnType());
221 if (return_type.IsValid())
222 return SBType(return_type);
224 return lldb::SBType();
227 lldb::SBTypeList SBType::GetFunctionArgumentTypes() {
228 SBTypeList sb_type_list;
230 CompilerType func_type(m_opaque_sp->GetCompilerType(true));
231 size_t count = func_type.GetNumberOfFunctionArguments();
232 for (size_t i = 0; i < count; i++) {
233 sb_type_list.Append(SBType(func_type.GetFunctionArgumentAtIndex(i)));
239 uint32_t SBType::GetNumberOfMemberFunctions() {
241 return m_opaque_sp->GetCompilerType(true).GetNumMemberFunctions();
246 lldb::SBTypeMemberFunction SBType::GetMemberFunctionAtIndex(uint32_t idx) {
247 SBTypeMemberFunction sb_func_type;
249 sb_func_type.reset(new TypeMemberFunctionImpl(
250 m_opaque_sp->GetCompilerType(true).GetMemberFunctionAtIndex(idx)));
254 lldb::SBType SBType::GetUnqualifiedType() {
257 return SBType(TypeImplSP(new TypeImpl(m_opaque_sp->GetUnqualifiedType())));
260 lldb::SBType SBType::GetCanonicalType() {
262 return SBType(TypeImplSP(new TypeImpl(m_opaque_sp->GetCanonicalType())));
266 lldb::BasicType SBType::GetBasicType() {
268 return m_opaque_sp->GetCompilerType(false).GetBasicTypeEnumeration();
269 return eBasicTypeInvalid;
272 SBType SBType::GetBasicType(lldb::BasicType basic_type) {
273 if (IsValid() && m_opaque_sp->IsValid())
275 m_opaque_sp->GetTypeSystem(false)->GetBasicTypeFromAST(basic_type));
279 uint32_t SBType::GetNumberOfDirectBaseClasses() {
281 return m_opaque_sp->GetCompilerType(true).GetNumDirectBaseClasses();
285 uint32_t SBType::GetNumberOfVirtualBaseClasses() {
287 return m_opaque_sp->GetCompilerType(true).GetNumVirtualBaseClasses();
291 uint32_t SBType::GetNumberOfFields() {
293 return m_opaque_sp->GetCompilerType(true).GetNumFields();
297 bool SBType::GetDescription(SBStream &description,
298 lldb::DescriptionLevel description_level) {
299 Stream &strm = description.ref();
302 m_opaque_sp->GetDescription(strm, description_level);
304 strm.PutCString("No value");
309 SBTypeMember SBType::GetDirectBaseClassAtIndex(uint32_t idx) {
310 SBTypeMember sb_type_member;
312 uint32_t bit_offset = 0;
313 CompilerType base_class_type =
314 m_opaque_sp->GetCompilerType(true).GetDirectBaseClassAtIndex(
316 if (base_class_type.IsValid())
317 sb_type_member.reset(new TypeMemberImpl(
318 TypeImplSP(new TypeImpl(base_class_type)), bit_offset));
320 return sb_type_member;
323 SBTypeMember SBType::GetVirtualBaseClassAtIndex(uint32_t idx) {
324 SBTypeMember sb_type_member;
326 uint32_t bit_offset = 0;
327 CompilerType base_class_type =
328 m_opaque_sp->GetCompilerType(true).GetVirtualBaseClassAtIndex(
330 if (base_class_type.IsValid())
331 sb_type_member.reset(new TypeMemberImpl(
332 TypeImplSP(new TypeImpl(base_class_type)), bit_offset));
334 return sb_type_member;
337 SBTypeEnumMemberList SBType::GetEnumMembers() {
338 SBTypeEnumMemberList sb_enum_member_list;
340 CompilerType this_type(m_opaque_sp->GetCompilerType(true));
341 if (this_type.IsValid()) {
342 this_type.ForEachEnumerator([&sb_enum_member_list](
343 const CompilerType &integer_type,
344 const ConstString &name,
345 const llvm::APSInt &value) -> bool {
346 SBTypeEnumMember enum_member(
347 lldb::TypeEnumMemberImplSP(new TypeEnumMemberImpl(
348 lldb::TypeImplSP(new TypeImpl(integer_type)), name, value)));
349 sb_enum_member_list.Append(enum_member);
350 return true; // Keep iterating
354 return sb_enum_member_list;
357 SBTypeMember SBType::GetFieldAtIndex(uint32_t idx) {
358 SBTypeMember sb_type_member;
360 CompilerType this_type(m_opaque_sp->GetCompilerType(false));
361 if (this_type.IsValid()) {
362 uint64_t bit_offset = 0;
363 uint32_t bitfield_bit_size = 0;
364 bool is_bitfield = false;
365 std::string name_sstr;
366 CompilerType field_type(this_type.GetFieldAtIndex(
367 idx, name_sstr, &bit_offset, &bitfield_bit_size, &is_bitfield));
368 if (field_type.IsValid()) {
370 if (!name_sstr.empty())
371 name.SetCString(name_sstr.c_str());
372 sb_type_member.reset(
373 new TypeMemberImpl(TypeImplSP(new TypeImpl(field_type)), bit_offset,
374 name, bitfield_bit_size, is_bitfield));
378 return sb_type_member;
381 bool SBType::IsTypeComplete() {
384 return m_opaque_sp->GetCompilerType(false).IsCompleteType();
387 uint32_t SBType::GetTypeFlags() {
390 return m_opaque_sp->GetCompilerType(true).GetTypeInfo();
393 const char *SBType::GetName() {
396 return m_opaque_sp->GetName().GetCString();
399 const char *SBType::GetDisplayTypeName() {
402 return m_opaque_sp->GetDisplayTypeName().GetCString();
405 lldb::TypeClass SBType::GetTypeClass() {
407 return m_opaque_sp->GetCompilerType(true).GetTypeClass();
408 return lldb::eTypeClassInvalid;
411 uint32_t SBType::GetNumberOfTemplateArguments() {
413 return m_opaque_sp->GetCompilerType(false).GetNumTemplateArguments();
417 lldb::SBType SBType::GetTemplateArgumentType(uint32_t idx) {
422 switch(GetTemplateArgumentKind(idx)) {
423 case eTemplateArgumentKindType:
424 type = m_opaque_sp->GetCompilerType(false).GetTypeTemplateArgument(idx);
426 case eTemplateArgumentKindIntegral:
427 type = m_opaque_sp->GetCompilerType(false)
428 .GetIntegralTemplateArgument(idx)
439 lldb::TemplateArgumentKind SBType::GetTemplateArgumentKind(uint32_t idx) {
441 return m_opaque_sp->GetCompilerType(false).GetTemplateArgumentKind(idx);
442 return eTemplateArgumentKindNull;
445 SBTypeList::SBTypeList() : m_opaque_ap(new TypeListImpl()) {}
447 SBTypeList::SBTypeList(const SBTypeList &rhs)
448 : m_opaque_ap(new TypeListImpl()) {
449 for (uint32_t i = 0, rhs_size = const_cast<SBTypeList &>(rhs).GetSize();
451 Append(const_cast<SBTypeList &>(rhs).GetTypeAtIndex(i));
454 bool SBTypeList::IsValid() { return (m_opaque_ap.get() != NULL); }
456 SBTypeList &SBTypeList::operator=(const SBTypeList &rhs) {
458 m_opaque_ap.reset(new TypeListImpl());
459 for (uint32_t i = 0, rhs_size = const_cast<SBTypeList &>(rhs).GetSize();
461 Append(const_cast<SBTypeList &>(rhs).GetTypeAtIndex(i));
466 void SBTypeList::Append(SBType type) {
468 m_opaque_ap->Append(type.m_opaque_sp);
471 SBType SBTypeList::GetTypeAtIndex(uint32_t index) {
472 if (m_opaque_ap.get())
473 return SBType(m_opaque_ap->GetTypeAtIndex(index));
477 uint32_t SBTypeList::GetSize() { return m_opaque_ap->GetSize(); }
479 SBTypeList::~SBTypeList() {}
481 SBTypeMember::SBTypeMember() : m_opaque_ap() {}
483 SBTypeMember::~SBTypeMember() {}
485 SBTypeMember::SBTypeMember(const SBTypeMember &rhs) : m_opaque_ap() {
488 m_opaque_ap.reset(new TypeMemberImpl(rhs.ref()));
492 lldb::SBTypeMember &SBTypeMember::operator=(const lldb::SBTypeMember &rhs) {
495 m_opaque_ap.reset(new TypeMemberImpl(rhs.ref()));
500 bool SBTypeMember::IsValid() const { return m_opaque_ap.get(); }
502 const char *SBTypeMember::GetName() {
503 if (m_opaque_ap.get())
504 return m_opaque_ap->GetName().GetCString();
508 SBType SBTypeMember::GetType() {
510 if (m_opaque_ap.get()) {
511 sb_type.SetSP(m_opaque_ap->GetTypeImpl());
516 uint64_t SBTypeMember::GetOffsetInBytes() {
517 if (m_opaque_ap.get())
518 return m_opaque_ap->GetBitOffset() / 8u;
522 uint64_t SBTypeMember::GetOffsetInBits() {
523 if (m_opaque_ap.get())
524 return m_opaque_ap->GetBitOffset();
528 bool SBTypeMember::IsBitfield() {
529 if (m_opaque_ap.get())
530 return m_opaque_ap->GetIsBitfield();
534 uint32_t SBTypeMember::GetBitfieldSizeInBits() {
535 if (m_opaque_ap.get())
536 return m_opaque_ap->GetBitfieldBitSize();
540 bool SBTypeMember::GetDescription(lldb::SBStream &description,
541 lldb::DescriptionLevel description_level) {
542 Stream &strm = description.ref();
544 if (m_opaque_ap.get()) {
545 const uint32_t bit_offset = m_opaque_ap->GetBitOffset();
546 const uint32_t byte_offset = bit_offset / 8u;
547 const uint32_t byte_bit_offset = bit_offset % 8u;
548 const char *name = m_opaque_ap->GetName().GetCString();
550 strm.Printf("+%u + %u bits: (", byte_offset, byte_bit_offset);
552 strm.Printf("+%u: (", byte_offset);
554 TypeImplSP type_impl_sp(m_opaque_ap->GetTypeImpl());
556 type_impl_sp->GetDescription(strm, description_level);
558 strm.Printf(") %s", name);
559 if (m_opaque_ap->GetIsBitfield()) {
560 const uint32_t bitfield_bit_size = m_opaque_ap->GetBitfieldBitSize();
561 strm.Printf(" : %u", bitfield_bit_size);
564 strm.PutCString("No value");
569 void SBTypeMember::reset(TypeMemberImpl *type_member_impl) {
570 m_opaque_ap.reset(type_member_impl);
573 TypeMemberImpl &SBTypeMember::ref() {
574 if (m_opaque_ap.get() == NULL)
575 m_opaque_ap.reset(new TypeMemberImpl());
576 return *m_opaque_ap.get();
579 const TypeMemberImpl &SBTypeMember::ref() const { return *m_opaque_ap.get(); }
581 SBTypeMemberFunction::SBTypeMemberFunction() : m_opaque_sp() {}
583 SBTypeMemberFunction::~SBTypeMemberFunction() {}
585 SBTypeMemberFunction::SBTypeMemberFunction(const SBTypeMemberFunction &rhs)
586 : m_opaque_sp(rhs.m_opaque_sp) {}
588 lldb::SBTypeMemberFunction &SBTypeMemberFunction::
589 operator=(const lldb::SBTypeMemberFunction &rhs) {
591 m_opaque_sp = rhs.m_opaque_sp;
595 bool SBTypeMemberFunction::IsValid() const { return m_opaque_sp.get(); }
597 const char *SBTypeMemberFunction::GetName() {
599 return m_opaque_sp->GetName().GetCString();
603 const char *SBTypeMemberFunction::GetDemangledName() {
605 ConstString mangled_str = m_opaque_sp->GetMangledName();
607 Mangled mangled(mangled_str, true);
608 return mangled.GetDemangledName(mangled.GuessLanguage()).GetCString();
614 const char *SBTypeMemberFunction::GetMangledName() {
616 return m_opaque_sp->GetMangledName().GetCString();
620 SBType SBTypeMemberFunction::GetType() {
623 sb_type.SetSP(lldb::TypeImplSP(new TypeImpl(m_opaque_sp->GetType())));
628 lldb::SBType SBTypeMemberFunction::GetReturnType() {
631 sb_type.SetSP(lldb::TypeImplSP(new TypeImpl(m_opaque_sp->GetReturnType())));
636 uint32_t SBTypeMemberFunction::GetNumberOfArguments() {
638 return m_opaque_sp->GetNumArguments();
642 lldb::SBType SBTypeMemberFunction::GetArgumentTypeAtIndex(uint32_t i) {
646 lldb::TypeImplSP(new TypeImpl(m_opaque_sp->GetArgumentAtIndex(i))));
651 lldb::MemberFunctionKind SBTypeMemberFunction::GetKind() {
653 return m_opaque_sp->GetKind();
654 return lldb::eMemberFunctionKindUnknown;
657 bool SBTypeMemberFunction::GetDescription(
658 lldb::SBStream &description, lldb::DescriptionLevel description_level) {
659 Stream &strm = description.ref();
662 return m_opaque_sp->GetDescription(strm);
667 void SBTypeMemberFunction::reset(TypeMemberFunctionImpl *type_member_impl) {
668 m_opaque_sp.reset(type_member_impl);
671 TypeMemberFunctionImpl &SBTypeMemberFunction::ref() {
673 m_opaque_sp.reset(new TypeMemberFunctionImpl());
674 return *m_opaque_sp.get();
677 const TypeMemberFunctionImpl &SBTypeMemberFunction::ref() const {
678 return *m_opaque_sp.get();